adds caching to _getOtherAccountIds too
authorLars Kneschke <l.kneschke@metaways.de>
Wed, 15 Apr 2015 03:16:57 +0000 (05:16 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 17 Apr 2015 17:29:30 +0000 (19:29 +0200)
... as this is called very often via CalDAV

* and adds method name to (zend-)cache id
* adds verbose logging switch to Tinebase_Cache_PerRequest

Change-Id: Ic29a25a3df7f3eefc27e17bfe932fc464d476e89
Reviewed-on: http://gerrit.tine20.com/customers/1835
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Lars Kneschke <l.kneschke@metaways.de>
tests/tine20/Tinebase/ContainerTest.php
tine20/Tinebase/Cache/PerRequest.php
tine20/Tinebase/Container.php

index 08bb0cf..731679f 100644 (file)
@@ -561,7 +561,9 @@ class Tinebase_ContainerTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(1, $otherUsers->filter('accountId', $user2->accountId)->count());
         
         Tinebase_User::getInstance()->setStatus($user2, 'disabled');
-        
+
+        Tinebase_Cache_PerRequest::getInstance()->resetCache();
+
         $otherUsers = Tinebase_Container::getInstance()->getOtherUsers($user1, 'Calendar', array(
             Tinebase_Model_Grants::GRANT_READ
         ));
index e662aaf..3217ff4 100644 (file)
@@ -34,7 +34,16 @@ class Tinebase_Cache_PerRequest
      * TODO allow to configure this
      */
     protected $_usePersistentCache = false;
-    
+
+    /**
+     * verbose log
+     *
+     * @var bool
+     *
+     * TODO allow to configure this
+     */
+    protected $_verboseLog = false;
+
     /**
      * holds the instance of the singleton
      *
@@ -106,7 +115,7 @@ class Tinebase_Cache_PerRequest
         $this->_inMemoryCache[$class][$method][$cacheId] = $value;
 
         if ($this->_usePersistentCache) {
-            Tinebase_Core::getCache()->save($value, $cacheId);
+            Tinebase_Core::getCache()->save($value, $method . $cacheId);
         }
         
         return $this;
@@ -123,20 +132,34 @@ class Tinebase_Cache_PerRequest
      */
     public function load($class, $method, $cacheId)
     {
+        if ($this->_verboseLog) {
+            $traceException = new Exception();
+            Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__
+                . " class/method/cacheid: $class $method $cacheId stack: " . $traceException->getTraceAsString());
+        }
+
         $cacheId = sha1($cacheId);
-        
+
         if (!isset($this->_inMemoryCache[$class]) ||
             !isset($this->_inMemoryCache[$class][$method]) ||
             !(isset($this->_inMemoryCache[$class][$method][$cacheId]) || array_key_exists($cacheId, $this->_inMemoryCache[$class][$method]))
         ) {
 
+            if ($this->_verboseLog) {
+                Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' ... not found in class cache');
+            }
+
             if ($this->_usePersistentCache) {
                 // TODO better use Tinebase_Core::getCache()->test() ?
-                $value = Tinebase_Core::getCache()->load($cacheId);
+                $value = Tinebase_Core::getCache()->load($method . $cacheId);
                 if ($value !== false) {
                     $this->_inMemoryCache[$class][$method][$cacheId] = $value;
                     return $value;
                 }
+
+                if ($this->_verboseLog) {
+                    Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' ... not found in zend cache');
+                }
             }
 
             throw new Tinebase_Exception_NotFound('cacheId not found');
@@ -173,8 +196,10 @@ class Tinebase_Cache_PerRequest
             $this->_inMemoryCache[$class][$method] = array();
             
             return $this;
-        } else if ($this->_usePersistentCache) {
-            Tinebase_Core::getCache()->remove($cacheId);
+        }
+
+        if ($this->_usePersistentCache && $method && $cacheId) {
+            Tinebase_Core::getCache()->remove($method . $cacheId);
         }
         
         if (isset($this->_inMemoryCache[$class]) &&
@@ -186,4 +211,4 @@ class Tinebase_Cache_PerRequest
         
         return $this;
     }
-}
\ No newline at end of file
+}
index 298d594..cf548a3 100644 (file)
@@ -843,7 +843,14 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
         $accountId   = Tinebase_Model_User::convertUserIdToInt($_accountId);
         $application = Tinebase_Application::getInstance()->getApplicationByName($_application);
         $grant       = $_ignoreACL ? '*' : $_grant;
-        
+
+        $classCacheId = convertCacheId($accountId . $application->getId() . implode('', (array)$grant) .(int)$_ignoreACL);
+        try {
+            return $this->loadFromClassCache(__FUNCTION__, $classCacheId);
+        } catch (Tinebase_Exception_NotFound $tenf) {
+            // continue...
+        }
+
         // first grab all container ids ...
         $select = $this->_db->select()
             ->distinct()
@@ -888,6 +895,8 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
             
         $stmt = $this->_db->query('/*' . __FUNCTION__ . '*/' . $select);
         $accountIds = $stmt->fetchAll(Zend_Db::FETCH_COLUMN);
+
+        $this->saveInClassCache(__FUNCTION__, $classCacheId, $accountIds);
         
         return $accountIds;
     }
@@ -975,7 +984,7 @@ class Tinebase_Container extends Tinebase_Backend_Sql_Abstract
         $stmt = $this->_db->query('/*' . __FUNCTION__ . '*/' . $select);
         
         $containers = new Tinebase_Record_RecordSet('Tinebase_Model_Container', $stmt->fetchAll(Zend_Db::FETCH_ASSOC), TRUE);
-        
+
         $this->saveInClassCache(__FUNCTION__, $classCacheId, $containers);
         
         return $containers;