]> source.dussan.org Git - nextcloud-server.git/commitdiff
The group database backend should cache groups
authorRoeland Jago Douma <roeland@famdouma.nl>
Sun, 25 Oct 2015 08:45:51 +0000 (09:45 +0100)
committerRoeland Jago Douma <rullzer@owncloud.com>
Tue, 12 Jan 2016 07:24:08 +0000 (08:24 +0100)
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

lib/private/group/database.php

index ad6174808bbfb2feece9e77e15c026ad2dd8e775..f1aa72d2c5f5677ce3e662951208e2cca0b26607 100644 (file)
@@ -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) {