diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2015-10-25 09:45:51 +0100 |
---|---|---|
committer | Roeland Jago Douma <rullzer@owncloud.com> | 2016-01-12 08:24:08 +0100 |
commit | 53e37804ec67779a3582ce3ce1ad964dcf311bda (patch) | |
tree | ffa398606f54c4d701005e92bbc170c4e0d2383a /lib/private/group | |
parent | 44e91bb90ad1b714f4e0c04e22501825e2d6f01f (diff) | |
download | nextcloud-server-53e37804ec67779a3582ce3ce1ad964dcf311bda.tar.gz nextcloud-server-53e37804ec67779a3582ce3ce1ad964dcf311bda.zip |
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
Diffstat (limited to 'lib/private/group')
-rw-r--r-- | lib/private/group/database.php | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/lib/private/group/database.php b/lib/private/group/database.php index ad6174808bb..f1aa72d2c5f 100644 --- a/lib/private/group/database.php +++ b/lib/private/group/database.php @@ -50,6 +50,9 @@ */ class OC_Group_Database extends OC_Group_Backend { + /** @var string[] */ + private $groupCache = []; + /** * Try to create a new group * @param string $gid The name of the group to create @@ -59,21 +62,36 @@ class OC_Group_Database extends OC_Group_Backend { * be returned. */ public function createGroup( $gid ) { - // Check for existence - $stmt = OC_DB::prepare( "SELECT `gid` FROM `*PREFIX*groups` WHERE `gid` = ?" ); - $result = $stmt->execute( array( $gid )); - - if( $result->fetchRow() ) { - // Can not add an existing group + // 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` = ?" ); + $result = $stmt->execute( [$gid] ); + + if( $result->fetchRow() ) { + // Can not add an existing group + + // Add to cache + $this->groupCache[$gid] = $gid; + + return false; + } } - else{ - // Add group and exit - $stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*groups` ( `gid` ) VALUES( ? )" ); - $result = $stmt->execute( array( $gid )); - return $result ? true : false; + // Add group and exit + $stmt = OC_DB::prepare( "INSERT INTO `*PREFIX*groups` ( `gid` ) VALUES( ? )" ); + $result = $stmt->execute( [$gid] ); + + 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->execute( array( $gid )); + // Delete from cache + unset($this->groupCache[$gid]); + 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` = ?" ); $result = $stmt->execute( array( $uid )); - $groups = array(); + $groups = []; while( $row = $result->fetchRow()) { $groups[] = $row["gid"]; + $this->groupCache[$row['gid']] = $row['gid']; } return $groups; @@ -202,6 +224,11 @@ class OC_Group_Database extends OC_Group_Backend { * @return bool */ 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` = ?'); $result = $query->execute(array($gid))->fetchOne(); if ($result !== false) { |