forked from zendframework/zend-paginator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(experimental) Move Cache instance to Adapter impl
see zendframework#9
- Loading branch information
Showing
5 changed files
with
268 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Paginator\Adapter; | ||
|
||
use Zend\Cache\Storage\StorageInterface as CacheStorage; | ||
|
||
class CacheAdapter implements AdapterInterface | ||
{ | ||
/** | ||
* The cache tag prefix used to namespace Paginator results in the cache | ||
* | ||
*/ | ||
const CACHE_TAG_PREFIX = 'Zend_Paginator_'; | ||
|
||
/** | ||
* @var AdapterInterface | ||
*/ | ||
private $adapter; | ||
|
||
/** | ||
* @var CacheStorage; | ||
*/ | ||
private $cache; | ||
|
||
public function __construct(AdapterInterface $adapter, CacheStorage $cache) | ||
{ | ||
$this->adapter = $adapter; | ||
$this->cache = $cache; | ||
} | ||
|
||
public function getItems($offset, $itemCountPerPage) | ||
{ | ||
$pageNumber = ($offset === 0 ) ? 1 : $offset / $itemCountPerPage + 1; | ||
|
||
$cacheId = $this->getCacheId($pageNumber, $itemCountPerPage); | ||
|
||
if ($this->cache->hasItem($cacheId)) { | ||
return $this->cache->getItem($cacheId); | ||
} | ||
|
||
$items = $this->adapter->getItems($offset, $itemCountPerPage); | ||
|
||
if (!$items instanceof \Traversable) { | ||
$items = new \ArrayIterator($items); | ||
} | ||
|
||
$this->cache->setItem($cacheId, $items); | ||
|
||
return $items; | ||
} | ||
|
||
public function count() | ||
{ | ||
return $this->adapter->count(); | ||
} | ||
|
||
public function clearCache($pageNumber, $itemCountPerPage) | ||
{ | ||
$cacheId = $this->getCacheId($pageNumber, $itemCountPerPage); | ||
|
||
if ($this->cache->hasItem($cacheId)) { | ||
return $this->cache->removeItem($cacheId); | ||
} | ||
} | ||
|
||
protected function getCacheId($pageNumber, $itemCountPerPage) | ||
{ | ||
return static::CACHE_TAG_PREFIX . $pageNumber . '_' . $this->getCacheInternalId($itemCountPerPage); | ||
} | ||
|
||
/** | ||
* Get the internal cache id | ||
* Depends on the adapter and the item count per page | ||
* | ||
* Used to tag that unique Paginator instance in cache | ||
* | ||
* @return string | ||
*/ | ||
protected function getCacheInternalId($itemCountPerPage) | ||
{ | ||
return md5(serialize([ | ||
spl_object_hash($this->adapter), | ||
$itemCountPerPage | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace Zend\Paginator\Adapter; | ||
|
||
use Zend\Filter\FilterInterface; | ||
|
||
class FilterAdapter implements AdapterInterface | ||
{ | ||
/** | ||
* @var AdapterInterface | ||
*/ | ||
private $adapter; | ||
|
||
/** | ||
* @var FilterInterface; | ||
*/ | ||
private $filter; | ||
|
||
public function __construct(AdapterInterface $adapter, FilterInterface $filter) | ||
{ | ||
$this->adapter = $adapter; | ||
$this->filter = $filter; | ||
} | ||
|
||
public function getItems($offset, $itemCountPerPage) | ||
{ | ||
$items = $this->adapter->getItems($offset, $itemCountPerPage); | ||
return $this->filter->filter($items); | ||
} | ||
|
||
public function count() | ||
{ | ||
return $this->adapter->count(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Paginator; | ||
|
||
use Zend\Cache\Storage\IteratorInterface as CacheIterator; | ||
use Zend\Cache\Storage\StorageInterface as CacheStorage; | ||
|
||
use Zend\Paginator\Adapter\CacheAdapter; | ||
|
||
trait CacheTrait | ||
{ | ||
|
||
/** | ||
* Enable or disable the cache by Zend\Paginator\Paginator instance | ||
* | ||
* @var bool | ||
*/ | ||
protected $cacheEnabled = true; | ||
|
||
/** | ||
* Cache object | ||
* | ||
* @var CacheStorage | ||
*/ | ||
protected $cache; | ||
|
||
/** | ||
* Sets a cache object | ||
* | ||
* @param CacheStorage $cache | ||
*/ | ||
public function setCache(CacheStorage $cache) | ||
{ | ||
$this->cache = $cache; | ||
} | ||
|
||
/** | ||
* Enables/Disables the cache for this instance | ||
* | ||
* @param bool $enable | ||
* @return Paginator | ||
*/ | ||
public function setCacheEnabled($enable) | ||
{ | ||
$this->cacheEnabled = (bool) $enable; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Tells if there is an active cache object | ||
* and if the cache has not been disabled | ||
* | ||
* @return bool | ||
*/ | ||
protected function cacheEnabled() | ||
{ | ||
return (($this->cache instanceof CacheStorage) && $this->cacheEnabled); | ||
} | ||
|
||
/** | ||
* Returns the page item cache. | ||
* | ||
* @return array | ||
*/ | ||
public function getPageItemCache() | ||
{ | ||
$data = []; | ||
if ($this->cacheEnabled()) { | ||
$prefixLength = strlen(CacheAdapter::CACHE_TAG_PREFIX); | ||
$cacheIterator = $this->cache->getIterator(); | ||
$cacheIterator->setMode(CacheIterator::CURRENT_AS_VALUE); | ||
foreach ($cacheIterator as $key => $value) { | ||
if (substr($key, 0, $prefixLength) == CacheAdapter::CACHE_TAG_PREFIX) { | ||
$pageNumber = (int)substr($key, $prefixLength); | ||
$data[$pageNumber] = $value; | ||
} | ||
} | ||
} | ||
return $data; | ||
} | ||
|
||
/** | ||
* Clear the page item cache. | ||
* | ||
* @param int $pageNumber | ||
* @return Paginator | ||
*/ | ||
public function clearPageItemCache($pageNumber = null) | ||
{ | ||
if (!$this->cacheEnabled()) { | ||
return $this; | ||
} | ||
|
||
$cacheAdapter = new CacheAdapter($this->adapter, $this->cache); | ||
|
||
if (null === $pageNumber) { | ||
$prefixLength = strlen(CacheAdapter::CACHE_TAG_PREFIX); | ||
$cacheIterator = $this->cache->getIterator(); | ||
$cacheIterator->setMode(CacheIterator::CURRENT_AS_KEY); | ||
foreach ($cacheIterator as $key) { | ||
if (substr($key, 0, $prefixLength) == CacheAdapter::CACHE_TAG_PREFIX) { | ||
$pageNumber = (int)substr($key, $prefixLength); | ||
$cacheAdapter->clearCache($pageNumber, $this->getItemCountPerPage()); | ||
} | ||
} | ||
} else { | ||
$pageNumber = $this->normalizePageNumber($pageNumber); | ||
|
||
$cacheAdapter->clearCache($pageNumber, $this->getItemCountPerPage()); | ||
} | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters