The group database backend should cache groups
This avoids duplicated queries like first checking the group_users db and then just doing a select on the group db. Those enries are linked (and should be using foreign keys!) This commit makes sure we cache those entries. If a user is part of N groups this saves N queries on webdav access
This commit is contained in:
parent
44e91bb90a
commit
53e37804ec
|
@ -50,6 +50,9 @@
|
||||||
*/
|
*/
|
||||||
class OC_Group_Database extends OC_Group_Backend {
|
class OC_Group_Database extends OC_Group_Backend {
|
||||||
|
|
||||||
|
/** @var string[] */
|
||||||
|
private $groupCache = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Try to create a new group
|
* Try to create a new group
|
||||||
* @param string $gid The name of the group to create
|
* @param string $gid The name of the group to create
|
||||||
|
@ -59,21 +62,36 @@ class OC_Group_Database extends OC_Group_Backend {
|
||||||
* be returned.
|
* be returned.
|
||||||
*/
|
*/
|
||||||
public function createGroup( $gid ) {
|
public function createGroup( $gid ) {
|
||||||
// Check for existence
|
// Check cache first
|
||||||
|
if (isset($this->groupCache[$gid])) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
// Check for existence in DB
|
||||||
$stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?" );
|
$stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?" );
|
||||||
$result = $stmt->execute( array( $gid ));
|
$result = $stmt->execute( [$gid] );
|
||||||
|
|
||||||
if( $result->fetchRow() ) {
|
if( $result->fetchRow() ) {
|
||||||
// Can not add an existing group
|
// Can not add an existing group
|
||||||
|
|
||||||
|
// Add to cache
|
||||||
|
$this->groupCache[$gid] = $gid;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else{
|
}
|
||||||
|
|
||||||
// Add group and exit
|
// Add group and exit
|
||||||
$stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*groups` ( `gid` ) VALUES( ? )" );
|
$stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*groups` ( `gid` ) VALUES( ? )" );
|
||||||
$result = $stmt->execute( array( $gid ));
|
$result = $stmt->execute( [$gid] );
|
||||||
|
|
||||||
return $result ? true : false;
|
if (!$result) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add to cache
|
||||||
|
$this->groupCache[$gid] = $gid;
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -96,6 +114,9 @@ class OC_Group_Database extends OC_Group_Backend {
|
||||||
$stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ?" );
|
$stmt = OC_DB::prepare( "DELETE FROM `*PREFIX*group_admin` WHERE `gid` = ?" );
|
||||||
$stmt->execute( array( $gid ));
|
$stmt->execute( array( $gid ));
|
||||||
|
|
||||||
|
// Delete from cache
|
||||||
|
unset($this->groupCache[$gid]);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,9 +183,10 @@ class OC_Group_Database extends OC_Group_Backend {
|
||||||
$stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*group_user` WHERE `uid` = ?" );
|
$stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*group_user` WHERE `uid` = ?" );
|
||||||
$result = $stmt->execute( array( $uid ));
|
$result = $stmt->execute( array( $uid ));
|
||||||
|
|
||||||
$groups = array();
|
$groups = [];
|
||||||
while( $row = $result->fetchRow()) {
|
while( $row = $result->fetchRow()) {
|
||||||
$groups[] = $row["gid"];
|
$groups[] = $row["gid"];
|
||||||
|
$this->groupCache[$row['gid']] = $row['gid'];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $groups;
|
return $groups;
|
||||||
|
@ -202,6 +224,11 @@ class OC_Group_Database extends OC_Group_Backend {
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function groupExists($gid) {
|
public function groupExists($gid) {
|
||||||
|
// Check cache first
|
||||||
|
if (isset($this->groupCache[$gid])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
$query = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?');
|
$query = OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?');
|
||||||
$result = $query->execute(array($gid))->fetchOne();
|
$result = $query->execute(array($gid))->fetchOne();
|
||||||
if ($result !== false) {
|
if ($result !== false) {
|
||||||
|
|
Loading…
Reference in New Issue