From: Gunnar Wrobel Date: Mon, 3 Jan 2011 17:11:15 +0000 (+0100) Subject: Completed extraction of the list cache. X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=8dec03f8a579a3aa9b47d5179abc80ed70aba621;p=horde.git Completed extraction of the list cache. --- diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache/List.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache/List.php index 084d7b161..449a65dc3 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache/List.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Cache/List.php @@ -27,20 +27,143 @@ */ 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() + ); + } } diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Decorator/Cache.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Decorator/Cache.php index daa0987f2..8be9402fb 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Decorator/Cache.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/Decorator/Cache.php @@ -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 + ) ); } diff --git a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Cache.php b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Cache.php index 14a3966e6..83546ec9b 100644 --- a/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Cache.php +++ b/framework/Kolab_Storage/lib/Horde/Kolab/Storage/List/Decorator/Cache.php @@ -28,24 +28,6 @@ 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 diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php index 04a1f63ce..7ef9a7193 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/TestCase.php @@ -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); diff --git a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php index 11e27e79c..b9d218d67 100644 --- a/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php +++ b/framework/Kolab_Storage/test/Horde/Kolab/Storage/Unit/List/Decorator/CacheTest.php @@ -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')