diff --git a/src/Paginator.php b/src/Paginator.php index 7ed5e52..99b298e 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -28,7 +28,7 @@ Iterator, IteratorAggregate, Traversable, - Zend\Cache\Frontend\Core as CacheCore, + Zend\Cache\Storage\Adapter as CacheAdapter, Zend\Db\Select as DbSelect, Zend\Db\Table\AbstractRowset as DbAbstractRowset, Zend\Db\Table\Select as DbTableSelect, @@ -96,7 +96,7 @@ class Paginator implements Countable, IteratorAggregate /** * Cache object * - * @var CacheCore + * @var CacheAdapter */ protected static $_cache; @@ -323,9 +323,9 @@ public static function setDefaultItemCountPerPage($count) /** * Sets a cache object * - * @param CacheCore $cache + * @param CacheAdapter $cache */ - public static function setCache(CacheCore $cache) + public static function setCache(CacheAdapter $cache) { self::$_cache = $cache; } @@ -476,14 +476,21 @@ public function clearPageItemCache($pageNumber = null) } if (null === $pageNumber) { - foreach (self::$_cache->getIdsMatchingTags(array($this->_getCacheInternalId())) as $id) { + self::$_cache->find(CacheAdapter::MATCH_TAGS_OR, array('tags' => array( + $this->_getCacheInternalId() + ))); + $cacheIds = array(); + while (($item = self::$_cache->fetch()) !== false) { + $cacheIds[] = $item['key']; + } + foreach ($cacheIds as $id) { if (preg_match('|'.self::CACHE_TAG_PREFIX."(\d+)_.*|", $id, $page)) { - self::$_cache->remove($this->_getCacheId($page[1])); + self::$_cache->removeItem($this->_getCacheId($page[1])); } } } else { $cleanId = $this->_getCacheId($pageNumber); - self::$_cache->remove($cleanId); + self::$_cache->removeItem($cleanId); } return $this; } @@ -676,7 +683,7 @@ public function getItemCount($items) if (is_array($items) || $items instanceof Countable) { $itemCount = count($items); - } else { // $items is something like LimitIterator + } elseif($items instanceof Traversable) { // $items is something like LimitIterator $itemCount = iterator_count($items); } @@ -693,7 +700,7 @@ public function getItemsByPage($pageNumber) $pageNumber = $this->normalizePageNumber($pageNumber); if ($this->_cacheEnabled()) { - $data = self::$_cache->load($this->_getCacheId($pageNumber)); + $data = self::$_cache->getItem($this->_getCacheId($pageNumber)); if ($data !== false) { return $data; } @@ -714,7 +721,11 @@ public function getItemsByPage($pageNumber) } if ($this->_cacheEnabled()) { - self::$_cache->save($items, $this->_getCacheId($pageNumber), array($this->_getCacheInternalId())); + self::$_cache->setItem( + $this->_getCacheId($pageNumber), + $items, + array('tags' => array($this->_getCacheInternalId())) + ); } return $items; @@ -802,10 +813,17 @@ public function getPageItemCache() { $data = array(); if ($this->_cacheEnabled()) { - foreach (self::$_cache->getIdsMatchingTags(array($this->_getCacheInternalId())) as $id) { - if (preg_match('|'.self::CACHE_TAG_PREFIX."(\d+)_.*|", $id, $page)) { - $data[$page[1]] = self::$_cache->load($this->_getCacheId($page[1])); - } + $cacheIds = self::$_cache->find(CacheAdapter::MATCH_TAGS_OR, array( + 'tags' => array($this->_getCacheInternalId()), + )); + $cacheIds = array(); + while (($item = self::$_cache->fetch()) !== false) { + $cacheIds[] = $item['key']; + } + foreach ($cacheIds as $id) { + if (preg_match('|'.self::CACHE_TAG_PREFIX."(\d+)_.*|", $id, $page)) { + $data[$page[1]] = self::$_cache->getItem($this->_getCacheId($page[1])); + } } } return $data; diff --git a/test/PaginatorTest.php b/test/PaginatorTest.php index ae95395..104ba67 100644 --- a/test/PaginatorTest.php +++ b/test/PaginatorTest.php @@ -25,12 +25,14 @@ namespace ZendTest\Paginator; use PHPUnit_Framework_TestCase as TestCase, - Zend\Paginator, - Zend\View\Helper, - Zend\View, + Zend\Cache\StorageFactory as CacheFactory, + Zend\Cache\Storage\Adapter as CacheAdapter, Zend\Config, + Zend\Paginator, Zend\Paginator\Adapter, - Zend\Paginator\Exception; + Zend\Paginator\Exception, + Zend\View, + Zend\View\Helper; /** @@ -77,10 +79,24 @@ protected function setUp() $this->_config = new Config\Xml(__DIR__ . '/_files/config.xml'); - $fO = array('lifetime' => 3600, 'automatic_serialization' => true); - $bO = array('cache_dir'=> $this->_getTmpDir()); - - $this->_cache = \Zend\Cache\Cache::factory('Core', 'File', $fO, $bO); + $this->_cache = CacheFactory::factory(array( + 'adapter' => array( + 'name' => 'filesystem', + 'options' => array( + 'ttl' => 3600, + 'cache_dir' => $this->_getTmpDir(), + ), + ), + 'plugins' => array( + array( + 'name' => 'serializer', + 'options' => array( + 'serializer' => 'php_serialize', + ), + ), + ), + )); + $this->_cache->clear(CacheAdapter::MATCH_ALL); Paginator\Paginator::setCache($this->_cache); @@ -89,6 +105,7 @@ protected function setUp() protected function tearDown() { + $this->_cache->clear(CacheAdapter::MATCH_ALL); $this->_dbConn = null; $this->_testCollection = null; $this->_paginator = null; @@ -97,10 +114,6 @@ protected function tearDown() protected function _getTmpDir() { $tmpDir = rtrim(sys_get_temp_dir(), '/\\') . DIRECTORY_SEPARATOR . 'zend_paginator'; - if (file_exists($tmpDir)) { - $this->_rmDirRecursive($tmpDir); - } - mkdir($tmpDir); $this->cacheDir = $tmpDir; return $tmpDir; } @@ -136,7 +149,7 @@ protected function _restorePaginatorDefaults() Paginator\Paginator::setScrollingStyleBroker(new Paginator\ScrollingStyleBroker()); - $this->_cache->clean(); + $this->_cache->clear(CacheAdapter::MATCH_ALL); $this->_paginator->setCacheEnabled(true); } @@ -656,15 +669,16 @@ public function testCacheDoesNotDisturbResultsWhenChangingParam() $pageItems = $this->_paginator->setItemCountPerPage(8)->setCurrentPageNumber(3)->getCurrentItems(); $pageItems = $this->_paginator->getPageItemCache(); - $expected = array(3 => new \ArrayIterator(range(17, 24))); - $this->assertEquals($expected, $pageItems); + $expected = /*array(3 => */ new \ArrayIterator(range(17, 24)) /*) */; + $this->assertEquals($expected, $pageItems[3]); // get back to already cached data $this->_paginator->setItemCountPerPage(5); $pageItems = $this->_paginator->getPageItemCache(); $expected =array(1 => new \ArrayIterator(range(1, 5)), 2 => new \ArrayIterator(range(6, 10))); - $this->assertEquals($expected, $pageItems); + $this->assertEquals($expected[1], $pageItems[1]); + $this->assertEquals($expected[2], $pageItems[2]); } public function testToJson()