Store one list array in the cache.
authorGunnar Wrobel <p@rdus.de>
Mon, 3 Jan 2011 21:34:31 +0000 (22:34 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 4 Jan 2011 07:54:25 +0000 (08:54 +0100)
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache/List.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php

index b0c2f25..de48117 100644 (file)
@@ -177,27 +177,25 @@ class Horde_Kolab_Storage_Cache
      * Retrieve list data.
      *
      * @param string $connection_id ID of the connection matching the list.
-     * @param string $key           Access key to the cached data.
      *
      * @return mixed The data of the object.
      */
-    public function loadListData($connection_id, $key)
+    public function loadListData($connection_id)
     {
-        return $this->horde_cache->get($this->_getListKey($connection_id, $key), 0);
+        return $this->horde_cache->get($this->_getListKey($connection_id), 0);
     }
 
     /**
      * Cache list data.
      *
      * @param string $connection_id ID of the connection matching the list.
-     * @param string $key           Access key to the cached data.
      * @param string $data          The data to be cached.
      *
      * @return boolean True if successfull.
      */
-    public function storeListData($connection_id, $key, $data)
+    public function storeListData($connection_id, $data)
     {
-        $this->horde_cache->set($this->_getListKey($connection_id, $key), $data);
+        $this->horde_cache->set($this->_getListKey($connection_id), $data);
     }
 
     /**
@@ -208,9 +206,9 @@ class Horde_Kolab_Storage_Cache
      *
      * @return mixed The data of the object.
      */
-    private function _getListKey($connection_id, $key)
+    private function _getListKey($connection_id)
     {
-        return $connection_id . ':LIST:' . $key;
+        return $connection_id . ':LIST';
     }
 
     /**
index 449a65d..408d0ff 100644 (file)
@@ -60,6 +60,13 @@ class Horde_Kolab_Storage_Cache_List
     private $_list_id;
 
     /**
+     * The list data.
+     *
+     * @var array
+     */
+    private $_data = false;
+
+    /**
      * Constructor.
      *
      * @param Horde_Kolab_Storage_Cache $cache   The core cache driver.
@@ -83,23 +90,50 @@ class Horde_Kolab_Storage_Cache_List
     }
 
     /**
+     * Retrieve list data.
+     *
+     * @param string $key Access key to the cached data.
+     *
+     * @return mixed The data of the object.
+     */
+    private function _load($key)
+    {
+        $data = $this->_cache->loadListData($this->_list_id);
+        if (isset($data[$key])) {
+            return $data[$key];
+        }
+    }
+
+    /**
+     * Store list data.
+     *
+     * @param string $key  Access key to the cached data.
+     * @param mixed  $data Value to cache.
+     *
+     * @return NULL
+     */
+    private function _store($key, $data)
+    {
+        $stored = $this->_cache->loadListData($this->_list_id);
+        if (!is_array($stored)) {
+            $stored = array();
+        }
+        $stored[$key] = $data;
+        $this->_cache->storeListData($this->_list_id, $stored);
+    }
+
+    /**
      * Check if the cache has been initialized.
      *
      * @return boolean True if cache data is available.
      */
     public function isInitialized()
     {
-        $last_sync = $this->_cache->loadListData(
-            $this->_list_id,
-            self::SYNC
-        );
+        $last_sync = $this->_load(self::SYNC);
         if (empty($last_sync)) {
             return false;
         }
-        $version = $this->_cache->loadListData(
-            $this->_list_id,
-            self::VERSION
-        );
+        $version = $this->_load(self::VERSION);
         if ($version != self::FORMAT_VERSION) {
             return false;
         }
@@ -113,10 +147,7 @@ class Horde_Kolab_Storage_Cache_List
      */
     public function getFolders()
     {
-        return $this->_cache->loadListData(
-            $this->_list_id,
-            self::FOLDERS
-        );
+        return $this->_load(self::FOLDERS);
     }
 
     /**
@@ -127,10 +158,7 @@ class Horde_Kolab_Storage_Cache_List
      */
     public function getFolderTypes()
     {
-        return $this->_cache->loadListData(
-            $this->_list_id,
-            self::TYPES
-        );
+        return $this->_load(self::TYPES);
     }
 
     /**
@@ -140,30 +168,10 @@ class Horde_Kolab_Storage_Cache_List
      */
     public function store(array $folders = null, array $types = null)
     {
-        $this->_cache->storeListData(
-            $this->_list_id,
-            self::QUERIES,
-            array()
-        );
-        $this->_cache->storeListData(
-            $this->_list_id,
-            self::FOLDERS,
-            $folders
-        );
-        $this->_cache->storeListData(
-            $this->_list_id,
-            self::TYPES,
-            $types
-        );
-        $this->_cache->storeListData(
-            $this->_list_id,
-            self::VERSION,
-            self::FORMAT_VERSION
-        );
-        $this->_cache->storeListData(
-            $this->_list_id,
-            self::SYNC,
-            time()
-        );
+        $this->_store(self::QUERIES, array());
+        $this->_store(self::FOLDERS, $folders);
+        $this->_store(self::TYPES, $types);
+        $this->_store(self::VERSION, self::FORMAT_VERSION);
+        $this->_store(self::SYNC, time());
     }
 }
index b9d218d..1ad9366 100644 (file)
@@ -276,8 +276,7 @@ extends Horde_Kolab_Storage_TestCase
                 $cache
             )
         );
-        $cache->storeListData($list->getConnectionId(), 'S', time());
-        $cache->storeListData($list->getConnectionId(), 'V', '1');
+        $cache->storeListData($list->getConnectionId(), array('S' => time(), 'V' => '1'));
         $this->mockDriver->expects($this->never())
             ->method('getMailboxes') 
             ->will($this->returnValue(array('INBOX')));