Completed extraction of the list cache.
authorGunnar Wrobel <p@rdus.de>
Mon, 3 Jan 2011 17:11:15 +0000 (18:11 +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/List.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/Decorator/Cache.php
framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Cache.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php
framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php

index 084d7b1..449a65d 100644 (file)
  */
 class Horde_Kolab_Storage_Cache_List
 {
+    /** Key for the folder list. */
+    const FOLDERS = 'F';
+
+    /** Key for the type list. */
+    const TYPES = 'T';
+
+    /** Key for the last time the list was synchronized. */
+    const SYNC = 'S';
+
+    /** Holds query cache results. */
+    const QUERIES = 'Q';
+
+    /** Key for the cache format version. */
+    const VERSION = 'V';
+
+    /** Holds the version number of the cache format. */
+    const FORMAT_VERSION = '1';
+
     /**
      * The core cache driver.
      *
      * @var Horde_Kolab_Storage_Cache
      */
-    protected $_cache;
+    private $_cache;
+
+    /**
+     * List ID.
+     *
+     * @var string
+     */
+    private $_list_id;
 
     /**
      * Constructor.
      *
-     * @param Horde_Kolab_Storage_Cache $cache The core cache driver.
+     * @param Horde_Kolab_Storage_Cache $cache   The core cache driver.
      */
-    public function __construct($cache)
+    public function __construct(Horde_Kolab_Storage_Cache $cache)
     {
         $this->_cache = $cache;
     }
+
+    /**
+     * The the list ID.
+     *
+     * @param string                    $list_id The unique ID for the list
+     *                                           used when caching it.
+     *
+     * @return NULL
+     */
+    public function setListId($list_id)
+    {
+        $this->_list_id = $list_id;
+    }
+
+    /**
+     * 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
+        );
+        if (empty($last_sync)) {
+            return false;
+        }
+        $version = $this->_cache->loadListData(
+            $this->_list_id,
+            self::VERSION
+        );
+        if ($version != self::FORMAT_VERSION) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Returns the list of folders from the cache.
+     *
+     * @return array The list of folders, represented as a list of strings.
+     */
+    public function getFolders()
+    {
+        return $this->_cache->loadListData(
+            $this->_list_id,
+            self::FOLDERS
+        );
+    }
+
+    /**
+     * Returns the folder type annotation from the cache.
+     *
+     * @return array The list folder types with the folder names as key and the
+     *               folder type as values.
+     */
+    public function getFolderTypes()
+    {
+        return $this->_cache->loadListData(
+            $this->_list_id,
+            self::TYPES
+        );
+    }
+
+    /**
+     * Store the folder list and folder type annotations in the cache.
+     *
+     * @return NULL
+     */
+    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()
+        );
+    }
 }
index daa0987..8be9402 100644 (file)
@@ -66,7 +66,9 @@ implements Horde_Kolab_Storage
     {
         return new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->_storage->getList(),
-            $this->_cache
+            new Horde_Kolab_Storage_Cache_List(
+                $this->_cache
+            )
         );
     }
 
index 14a3966..83546ec 100644 (file)
 class Horde_Kolab_Storage_List_Decorator_Cache
 implements Horde_Kolab_Storage_List
 {
-    /** Key for the folder list. */
-    const FOLDERS = 'F';
-
-    /** Key for the type list. */
-    const TYPES = 'T';
-
-    /** Key for the last time the list was synchronized. */
-    const SYNC = 'S';
-
-    /** Holds query cache results. */
-    const QUERIES = 'Q';
-
-    /** Key for the cache format version. */
-    const VERSION = 'V';
-
-    /** Holds the version number of the cache format. */
-    const FORMAT_VERSION = '1';
-
     /**
      * Decorated list handler.
      *
@@ -54,13 +36,6 @@ implements Horde_Kolab_Storage_List
     private $_list;
 
     /**
-     * The cache.
-     *
-     * @var Horde_Kolab_Storage_Cache
-     */
-    private $_cache;
-
-    /**
      * The list cache.
      *
      * @var Horde_Kolab_Storage_Cache_List
@@ -77,17 +52,17 @@ implements Horde_Kolab_Storage_List
     /**
      * Constructor.
      *
-     * @param Horde_Kolab_Storage_List  $list  The original list handler.
-     * @param Horde_Kolab_Storage_Cache $cache The cache storing data for this
-     *                                         decorator.
+     * @param Horde_Kolab_Storage_List       $list  The original list handler.
+     * @param Horde_Kolab_Storage_Cache_List $cache The cache storing data for
+     *                                              this decorator.
      */
     public function __construct(
         Horde_Kolab_Storage_List $list,
-        Horde_Kolab_Storage_Cache $cache
+        Horde_Kolab_Storage_Cache_List $cache
     ) {
         $this->_list = $list;
-        $this->_cache = $cache;
-        $this->_list_cache = new Horde_Kolab_Storage_Cache_List($cache);
+        $this->_list_cache = $cache;
+        $this->_list_cache->setListId($this->_list->getConnectionId());
     }
 
     /**
@@ -110,19 +85,7 @@ implements Horde_Kolab_Storage_List
         if ($this->_init) {
             return;
         }
-        $last_sync = $this->_cache->loadListData(
-            $this->_list->getConnectionId(),
-            self::SYNC
-        );
-        if (empty($last_sync)) {
-            $this->synchronize();
-            return;
-        }
-        $version = $this->_cache->loadListData(
-            $this->_list->getConnectionId(),
-            self::VERSION
-        );
-        if ($version != self::FORMAT_VERSION) {
+        if (!$this->_list_cache->isInitialized()) {
             $this->synchronize();
         }
     }
@@ -135,10 +98,7 @@ implements Horde_Kolab_Storage_List
     public function listFolders()
     {
         $this->_init();
-        return $this->_cache->loadListData(
-            $this->_list->getConnectionId(),
-            self::FOLDERS
-        );
+        return $this->_list_cache->getFolders();
     }
 
     /**
@@ -150,10 +110,7 @@ implements Horde_Kolab_Storage_List
     public function listFolderTypes()
     {
         $this->_init();
-        return $this->_cache->loadListData(
-            $this->_list->getConnectionId(),
-            self::TYPES
-        );
+        return $this->_list_cache->getFolderTypes();
     }
 
     /**
@@ -165,31 +122,11 @@ implements Horde_Kolab_Storage_List
     {
         $this->_list->synchronize();
 
-        $this->_cache->storeListData(
-            $this->_list->getConnectionId(),
-            self::QUERIES,
-            array()
-        );
-        $this->_cache->storeListData(
-            $this->_list->getConnectionId(),
-            self::FOLDERS,
-            $this->_list->listFolders()
-        );
-        $this->_cache->storeListData(
-            $this->_list->getConnectionId(),
-            self::TYPES,
+        $this->_list_cache->store(
+            $this->_list->listFolders(),
             $this->_list->listFolderTypes()
         );
-        $this->_cache->storeListData(
-            $this->_list->getConnectionId(),
-            self::VERSION,
-            self::FORMAT_VERSION
-        );
-        $this->_cache->storeListData(
-            $this->_list->getConnectionId(),
-            self::SYNC,
-            time()
-        );
+
         $this->_init = true;
     }
 }
\ No newline at end of file
index 04a1f63..7ef9a71 100644 (file)
@@ -208,6 +208,13 @@ extends PHPUnit_Framework_TestCase
         return new Horde_Kolab_Storage_Cache(new Horde_Cache_Storage_Mock());
     }
 
+    protected function getMockListCache()
+    {
+        return new Horde_Kolab_Storage_Cache_List(
+            $this->getMockCache()
+        );
+    }
+
     protected function assertLogCount($count)
     {
         $this->assertEquals(count($this->logHandler->events), $count);
index 11e27e7..b9d218d 100644 (file)
@@ -39,7 +39,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getNullList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->assertType('array', $list->listFolders());
     }
@@ -48,7 +48,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getTwoFolderList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->assertEquals(
             array('INBOX', 'INBOX/a'),
@@ -60,7 +60,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getAnnotatedList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->assertEquals(
             array('INBOX', 'INBOX/a', 'INBOX/Calendar', 'INBOX/Contacts', 'INBOX/Notes', 'INBOX/Tasks'),
@@ -72,7 +72,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->mockDriver->expects($this->once())
             ->method('getMailboxes') 
@@ -87,7 +87,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->mockDriver->expects($this->once())
             ->method('getMailboxes') 
@@ -101,31 +101,32 @@ extends Horde_Kolab_Storage_TestCase
 
     public function testTwoCachedLists()
     {
-        $cache = $this->getMockCache();
-        $list = new Horde_Kolab_Storage_List_Decorator_Cache(
-            $this->getMockDriverList(),
-            $cache
-        );
+        $decorated = $this->getMockDriverList();
         $this->mockDriver->expects($this->once())
             ->method('getMailboxes') 
             ->will($this->returnValue(array('INBOX')));
-        $this->mockDriver->expects($this->any())
+        $this->mockDriver->expects($this->once())
             ->method('getId') 
-            ->will($this->returnValue(array('A')));
+            ->will($this->returnValue('A'));
+        $list = new Horde_Kolab_Storage_List_Decorator_Cache(
+            $decorated,
+            $this->getMockListCache()
+        );
+
         $mockDriver2 = $this->getMock('Horde_Kolab_Storage_Driver');
+        $mockDriver2->expects($this->once())
+            ->method('getMailboxes') 
+            ->will($this->returnValue(array('NOTHING')));
+        $mockDriver2->expects($this->once())
+            ->method('getId') 
+            ->will($this->returnValue('B'));
         $list2 = new Horde_Kolab_Storage_List_Decorator_Cache(
             new Horde_Kolab_Storage_List_Base(
                 $mockDriver2,
                 new Horde_Kolab_Storage_Factory()
             ),
-            $cache
+            $this->getMockListCache()
         );
-        $mockDriver2->expects($this->once())
-            ->method('getMailboxes') 
-            ->will($this->returnValue(array('NOTHING')));
-        $this->mockDriver->expects($this->any())
-            ->method('getId') 
-            ->will($this->returnValue(array('B')));
 
         $list->listFolders();
         $list2->listFolders();
@@ -139,7 +140,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->mockDriver->expects($this->once())
             ->method('getMailboxes') 
@@ -151,7 +152,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->mockDriver->expects($this->once())
             ->method('getMailboxes') 
@@ -164,7 +165,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getNullList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->assertType('array', $list->listFolderTypes());
     }
@@ -173,7 +174,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getTwoFolderList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->assertEquals(
             array(),
@@ -185,7 +186,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getAnnotatedList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->assertEquals(
             array(
@@ -202,7 +203,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->mockDriver->expects($this->once())
             ->method('listAnnotation')
@@ -217,7 +218,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->mockDriver->expects($this->once())
             ->method('listAnnotation')
@@ -233,7 +234,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->mockDriver->expects($this->once())
             ->method('listAnnotation')
@@ -245,7 +246,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->mockDriver->expects($this->once())
             ->method('listAnnotation')
@@ -258,7 +259,7 @@ extends Horde_Kolab_Storage_TestCase
     {
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $this->getMockCache()
+            $this->getMockListCache()
         );
         $this->mockDriver->expects($this->once())
             ->method('listAnnotation')
@@ -271,7 +272,9 @@ extends Horde_Kolab_Storage_TestCase
         $cache = $this->getMockCache();
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $cache
+            new Horde_Kolab_Storage_Cache_List(
+                $cache
+            )
         );
         $cache->storeListData($list->getConnectionId(), 'S', time());
         $cache->storeListData($list->getConnectionId(), 'V', '1');
@@ -286,7 +289,9 @@ extends Horde_Kolab_Storage_TestCase
         $cache = $this->getMockCache();
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $cache
+            new Horde_Kolab_Storage_Cache_List(
+                $cache
+            )
         );
         $cache->storeListData($list->getConnectionId(), 'S', time());
         $cache->storeListData($list->getConnectionId(), 'V', '2');
@@ -304,7 +309,9 @@ extends Horde_Kolab_Storage_TestCase
         $cache = $this->getMockCache();
         $list = new Horde_Kolab_Storage_List_Decorator_Cache(
             $this->getMockDriverList(),
-            $cache
+            new Horde_Kolab_Storage_Cache_List(
+                $cache
+            )
         );
         $this->mockDriver->expects($this->once())
             ->method('getMailboxes')