开发者

Zend framework caching pagination results for different queries

开发者 https://www.devze.com 2023-01-06 05:12 出处:网络
Using Zend Paginator and the paginator cache works fine, but the same cached pages are returned for everything. Ie. I first look at a list of articles, when i go to view the categories the articles li

Using Zend Paginator and the paginator cache works fine, but the same cached pages are returned for everything. Ie. I first look at a list of articles, when i go to view the categories the articles list i开发者_Python百科s returned. How can I tell the paginator which result set i am looking for?

Also, how can I clear the paginated results without re-querying the paginator. Ie. I am updated a news article therefore the pagination needs to be cleared.

Thanks


Zend_Paginator uses two methods to define cache ID: _getCacheId and _getCacheInternalId. Second function is calculating cache ID based on two parameters: the number of items per page and special hash of the adapter object. The first function (_getCacheId) is calculating cache ID using result from _getCacheInternalId and current page.

So, if you are using two different paginator objects with 3 same internal parameters: adapter, current page number and the number of items per page, then your cache ID will be the same for these two objects.

So the only way I see is to define you own paginator class inherited from Zend_Paginator and to re-define one of these two internal functions to add a salt to cache ID. Something like this:

class My_Paginator extends Zend_Paginator {

    protected $_cacheSalt = '';

    public static function factory($data, $adapter = self::INTERNAL_ADAPTER, array $prefixPaths = null) {
    $paginator = parent::factory($data, $adapter, $prefixPaths);
    return new self($paginator->getAdapter());
}

    public function setCacheSalt($salt) {
        $this->_cacheSalt = $salt;
        return $this;
    }

    public function getCacheSalt() {
        return $this->_cacheSalt;
    }

    protected function _getCacheId($page = null) {
        $cacheSalt = $this->getCacheSalt();
        if ($cacheSalt != '') {
            $cacheSalt = '_' . $cacheSalt;
        }
        return parent::_getCacheId($page) . $cacheSalt;
    }
}

$articlesPaginator = My_Paginator::factory($articlesSelect, 'DbSelect');
$articlesPaginator->setCacheSalt('articles');

$categoriesSelect = My_Paginator::factory($categoriesSelect, 'DbSelect');
$articlesPaginator->setCacheSalt('categories');
0

精彩评论

暂无评论...
验证码 换一张
取 消