added in class cache to Tinebase_Group_Abstract/Sql
authorLars Kneschke <l.kneschke@metaways.de>
Sat, 20 Dec 2014 22:11:52 +0000 (23:11 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Wed, 7 Jan 2015 11:51:56 +0000 (12:51 +0100)
Change-Id: I641a22c1db8ac4970e021a6ec29533d9a2894060
Reviewed-on: http://gerrit.tine20.com/customers/1478
Reviewed-by: Lars Kneschke <l.kneschke@metaways.de>
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Tinebase/GroupTest.php
tine20/Tinebase/Group/Abstract.php
tine20/Tinebase/Group/Sql.php

index 9181c75..9399580 100644 (file)
@@ -5,16 +5,11 @@
  * @package     Tinebase
  * @subpackage  Group
  * @license     http://www.gnu.org/licenses/agpl.html
- * @copyright   Copyright (c) 2008-2013 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @copyright   Copyright (c) 2008-2015 Metaways Infosystems GmbH (http://www.metaways.de)
  * @author      Philipp Schüle <p.schuele@metaways.de>
  */
 
 /**
- * Test helper
- */
-require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'TestHelper.php';
-
-/**
  * Test class for Tinebase_Group
  */
 class Tinebase_GroupTest extends TestCase
@@ -32,6 +27,16 @@ class Tinebase_GroupTest extends TestCase
     protected $_removeGroupMembers = false;
     
     /**
+     * set up tests
+     */
+    protected function setUp()
+    {
+        parent::setUp();
+        
+        Tinebase_Group::getInstance()->resetClassCache();
+    }
+    
+    /**
      * try to add a group
      * 
      * @return Tinebase_Model_Group
index 0fb397a..d5edffd 100644 (file)
@@ -5,7 +5,7 @@
  * @package     Tinebase
  * @subpackage  Group
  * @license     http://www.gnu.org/licenses/agpl.html AGPL3
- * @copyright   Copyright (c) 2008 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @copyright   Copyright (c) 2008-2014 Metaways Infosystems GmbH (http://www.metaways.de)
  * @author      Lars Kneschke <l.kneschke@metaways.de>
  * 
  * @todo        add search count function
 abstract class Tinebase_Group_Abstract
 {
     /**
+     * in class cache 
+     * 
+     * @var array
+     */
+    protected $_classCache = array ();
+    
+    /**
      * return all groups an account is member of
      *
      * @param mixed $_accountId the account as integer or Tinebase_Model_User
@@ -64,6 +71,23 @@ abstract class Tinebase_Group_Abstract
     abstract public function removeGroupMember($_groupId, $_accountId);
     
     /**
+     * reset class cache
+     * 
+     * @param string $key
+     * @return Tinebase_Group_Sql
+     */
+    public function resetClassCache($key = null)
+    {
+        foreach ($this->_classCache as $cacheKey => $cacheValue) {
+            if ($key === null || $key === $cacheKey) {
+                $this->_classCache[$cacheKey] = array();
+            }
+        }
+        
+        return $this;
+    }
+    
+    /**
      * create a new group
      *
      * @param string $_groupName
index 3d24ae4..2dfa83c 100644 (file)
@@ -5,7 +5,7 @@
  * @package     Tinebase
  * @subpackage  Group
  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
- * @copyright   Copyright (c) 2008-2010 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @copyright   Copyright (c) 2008-2015 Metaways Infosystems GmbH (http://www.metaways.de)
  * @author      Lars Kneschke <l.kneschke@metaways.de>
  */
 
@@ -51,6 +51,15 @@ class Tinebase_Group_Sql extends Tinebase_Group_Abstract
     protected $_addressBookInstalled = false;
     
     /**
+     * in class cache 
+     * 
+     * @var array
+     */
+    protected $_classCache = array (
+        'getGroupMemberships' => array()
+    );
+    
+    /**
      * the constructor
      */
     public function __construct() 
@@ -83,24 +92,30 @@ class Tinebase_Group_Sql extends Tinebase_Group_Abstract
     {
         $accountId = Tinebase_Model_User::convertUserIdToInt($_accountId);
         
-        $cacheId = convertCacheId('groupMemberships' . $accountId);
+        $classCacheId = $accountId;
+        
+        if (isset($this->_classCache[__FUNCTION__][$classCacheId])) {
+            return $this->_classCache[__FUNCTION__][$classCacheId];
+        }
+        
+        $cacheId     = convertCacheId(__FUNCTION__ . $classCacheId);
         $memberships = Tinebase_Core::getCache()->load($cacheId);
-
+        
         if (! $memberships) {
-            $memberships = array();
-            $colName = $this->groupsTable->getAdapter()->quoteIdentifier('account_id');
-            $select = $this->groupMembersTable->select();
-            $select->where($colName . ' = ?', $accountId);
+            $select = $this->_db->select()
+                ->distinct()
+                ->from(array('group_members' => SQL_TABLE_PREFIX . 'group_members'), array('group_id'))
+                ->where($this->_db->quoteIdentifier('account_id') . ' = ?', $accountId);
             
-            $rows = $this->groupMembersTable->fetchAll($select);
+            $stmt = $this->_db->query($select);
+            
+            $memberships = $stmt->fetchAll(Zend_Db::FETCH_COLUMN);
             
-            foreach($rows as $membership) {
-                $memberships[] = $membership->group_id;
-            }
-
             Tinebase_Core::getCache()->save($memberships, $cacheId);
         }
-
+        
+        $this->_classCache[__FUNCTION__][$classCacheId] = $memberships;
+        
         return $memberships;
     }
     
@@ -114,7 +129,7 @@ class Tinebase_Group_Sql extends Tinebase_Group_Abstract
     {
         $groupId = Tinebase_Model_Group::convertGroupIdToInt($_groupId);
         
-        $cacheId = convertCacheId('groupMembers' . $groupId);
+        $cacheId = convertCacheId(__FUNCTION__ . $groupId);
         $members = Tinebase_Core::getCache()->load($cacheId);
 
         if (! $members) {
@@ -184,11 +199,11 @@ class Tinebase_Group_Sql extends Tinebase_Group_Abstract
                         . ' User with ID ' . $accountId . ' does not have an account!');
                 }
                 
-                $this->_clearCache(array('groupMemberships' => $accountId));
+                $this->_clearCache(array('getGroupMemberships' => $accountId));
             }
         }
         
-        $this->_clearCache(array('groupMembers' => $groupId));
+        $this->_clearCache(array('getGroupMembers' => $groupId));
     }
     
     /**
@@ -198,10 +213,14 @@ class Tinebase_Group_Sql extends Tinebase_Group_Abstract
      */
     protected function _clearCache($cacheIds = array())
     {
+        $cache = Tinebase_Core::getCache();
+        
         foreach ($cacheIds as $type => $id) {
             $cacheId = convertCacheId($type . $id);
-            Tinebase_Core::getCache()->remove($cacheId);
+            $cache->remove($cacheId);
         }
+        
+        $this->resetClassCache();
     }
     
     /**
@@ -309,8 +328,8 @@ class Tinebase_Group_Sql extends Tinebase_Group_Abstract
             $this->groupMembersTable->insert($data);
             
             $this->_clearCache(array(
-                'groupMembers'     => $groupId,
-                'groupMemberships' => $accountId,
+                'getGroupMembers'     => $groupId,
+                'getGroupMemberships' => $accountId,
             ));
         }
         
@@ -350,8 +369,8 @@ class Tinebase_Group_Sql extends Tinebase_Group_Abstract
         $this->groupMembersTable->delete($where);
         
         $this->_clearCache(array(
-            'groupMembers'     => $groupId,
-            'groupMemberships' => $accountId,
+            'getGroupMembers'     => $groupId,
+            'getGroupMemberships' => $accountId,
         ));
     }