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

Commit

Permalink
Merge branch 'cache_interfaces' of https://github.com/marc-mabe/zf2 i…
Browse files Browse the repository at this point in the history
…nto feature/cache-simplification
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
61 changes: 29 additions & 32 deletions src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
Iterator,
IteratorAggregate,
Traversable,
Zend\Cache\Storage\Adapter\AdapterInterface as CacheAdapter,
Zend\Cache\Storage\StorageInterface as CacheStorage,
Zend\Cache\Storage\IteratorInterface as CacheIterator,
Zend\Db\Table\AbstractRowset as DbAbstractRowset,
Zend\Db\Table\Select as DbTableSelect,
Zend\Db\Sql,
Expand Down Expand Up @@ -85,7 +86,7 @@ class Paginator implements Countable, IteratorAggregate
/**
* Cache object
*
* @var CacheAdapter
* @var CacheStorage
*/
protected static $_cache;

Expand Down Expand Up @@ -262,13 +263,13 @@ public static function setOptions($config)

self::$_config = $config;

if (isset($config['adapter_broker'])
if (isset($config['adapter_broker'])
&& null !== ($broker = $config['adapter_broker'])
) {
self::setAdapterBroker($broker);
}

if (isset($config['scrolling_style_broker'])
if (isset($config['scrolling_style_broker'])
&& null !== ($broker = $config['scrolling_style_broker'])
) {
self::setScrollingStyleBroker($broker);
Expand Down Expand Up @@ -314,9 +315,9 @@ public static function setDefaultItemCountPerPage($count)
/**
* Sets a cache object
*
* @param CacheAdapter $cache
* @param CacheStorage $cache
*/
public static function setCache(CacheAdapter $cache)
public static function setCache(CacheStorage $cache)
{
self::$_cache = $cache;
}
Expand Down Expand Up @@ -468,16 +469,15 @@ public function clearPageItemCache($pageNumber = null)
}

if (null === $pageNumber) {
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->removeItem($this->_getCacheId($page[1]));
$prefixLength = strlen(self::CACHE_TAG_PREFIX);
$cacheIterator = self::$_cache->getIterator();
$cacheIterator->setMode(CacheIterator::CURRENT_AS_KEY);
foreach ($cacheIterator as $key) {
$tags = self::$_cache->getTags($key);
if ($tags && in_array($this->_getCacheInternalId(), $tags)) {
if (substr($key, 0, $prefixLength) == self::CACHE_TAG_PREFIX) {
self::$_cache->removeItem($this->_getCacheId((int)substr($key, $prefixLength)));
}
}
}
} else {
Expand Down Expand Up @@ -715,11 +715,9 @@ public function getItemsByPage($pageNumber)
}

if ($this->_cacheEnabled()) {
self::$_cache->setItem(
$this->_getCacheId($pageNumber),
$items,
array('tags' => array($this->_getCacheInternalId()))
);
$cacheId = $this->_getCacheId($pageNumber);
self::$_cache->setItem($cacheId, $items);
self::$_cache->setTags($cacheId, array($this->_getCacheInternalId()));
}

return $items;
Expand Down Expand Up @@ -808,24 +806,23 @@ public function getPageItemCache()
{
$data = array();
if ($this->_cacheEnabled()) {
$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]));
$prefixLength = strlen(self::CACHE_TAG_PREFIX);
$cacheIterator = self::$_cache->getIterator();
$cacheIterator->setMode(CacheIterator::CURRENT_AS_VALUE);
foreach ($cacheIterator as $key => $value) {
$tags = self::$_cache->getTags($key);
if ($tags && in_array($this->_getCacheInternalId(), $tags)) {
if (substr($key, 0, $prefixLength) == self::CACHE_TAG_PREFIX) {
$data[(int)substr($key, $prefixLength)] = $value;
}
}
}
}
return $data;
}

/**
* Retrieves the view instance.
* Retrieves the view instance.
*
* If none registered, instantiates a PhpRenderer instance.
*
Expand Down
6 changes: 0 additions & 6 deletions test/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,13 @@ protected function setUp()
$this->_config = Config\Factory::fromFile(__DIR__ . '/_files/config.xml', true);

$this->_cache = CacheFactory::adapterFactory('memory', array('memory_limit' => 0));
$this->_cache->clear(CacheAdapter::MATCH_ALL);
Paginator\Paginator::setCache($this->_cache);

$this->_restorePaginatorDefaults();
}

protected function tearDown()
{
if ($this->_cache) {
$this->_cache->clear(CacheAdapter::MATCH_ALL);
$this->_cache = null;
}
$this->_dbConn = null;
$this->_testCollection = null;
$this->_paginator = null;
Expand Down Expand Up @@ -142,7 +137,6 @@ protected function _restorePaginatorDefaults()

Paginator\Paginator::setScrollingStyleBroker(new Paginator\ScrollingStyleBroker());

$this->_cache->clear(CacheAdapter::MATCH_ALL);
$this->_paginator->setCacheEnabled(true);
}

Expand Down

0 comments on commit 5cde84e

Please sign in to comment.