Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into hotfix/filter-validator-loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 30 deletions.
46 changes: 32 additions & 14 deletions src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -96,7 +96,7 @@ class Paginator implements Countable, IteratorAggregate
/**
* Cache object
*
* @var CacheCore
* @var CacheAdapter
*/
protected static $_cache;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
46 changes: 30 additions & 16 deletions test/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 999502e

Please sign in to comment.