*/
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()
+ );
+ }
}
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.
*
private $_list;
/**
- * The cache.
- *
- * @var Horde_Kolab_Storage_Cache
- */
- private $_cache;
-
- /**
* The list cache.
*
* @var Horde_Kolab_Storage_Cache_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());
}
/**
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();
}
}
public function listFolders()
{
$this->_init();
- return $this->_cache->loadListData(
- $this->_list->getConnectionId(),
- self::FOLDERS
- );
+ return $this->_list_cache->getFolders();
}
/**
public function listFolderTypes()
{
$this->_init();
- return $this->_cache->loadListData(
- $this->_list->getConnectionId(),
- self::TYPES
- );
+ return $this->_list_cache->getFolderTypes();
}
/**
{
$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
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getNullList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->assertType('array', $list->listFolders());
}
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getTwoFolderList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->assertEquals(
array('INBOX', 'INBOX/a'),
{
$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'),
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getMockDriverList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->mockDriver->expects($this->once())
->method('getMailboxes')
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getMockDriverList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->mockDriver->expects($this->once())
->method('getMailboxes')
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();
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getMockDriverList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->mockDriver->expects($this->once())
->method('getMailboxes')
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getMockDriverList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->mockDriver->expects($this->once())
->method('getMailboxes')
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getNullList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->assertType('array', $list->listFolderTypes());
}
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getTwoFolderList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->assertEquals(
array(),
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getAnnotatedList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->assertEquals(
array(
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getMockDriverList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->mockDriver->expects($this->once())
->method('listAnnotation')
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getMockDriverList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->mockDriver->expects($this->once())
->method('listAnnotation')
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getMockDriverList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->mockDriver->expects($this->once())
->method('listAnnotation')
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getMockDriverList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->mockDriver->expects($this->once())
->method('listAnnotation')
{
$list = new Horde_Kolab_Storage_List_Decorator_Cache(
$this->getMockDriverList(),
- $this->getMockCache()
+ $this->getMockListCache()
);
$this->mockDriver->expects($this->once())
->method('listAnnotation')
$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');
$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');
$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')