开发者

Zend Framework Paginator cache doesn't work (no hit)

开发者 https://www.devze.com 2023-04-06 01:25 出处:网络
I use a file based cache to cache a paginator: $table = $this->getDbTable(); $ret = $table->select()

I use a file based cache to cache a paginator:

$table = $this->getDbTable();

$ret = $table   ->select()
        ->from($table,array('id',
                            'UNIX_TIMESTAMP(`date`) as date',
                            'categoryId',
                            'title',
                            'teaser'))
        ->where('`categoryId`=?',$cat)
        ->order('date desc');

$adapter = new Zend_Paginator_Adapter_DbTableSelect($ret);
$paginator = new Zend_Paginator($adapter);

$fO = array('lifetime' => 3600, 'automatic_serialization' => true);
$bO = array('cache_dir'=>APPLICATION_PATH . '/cache');
$cache = Zend_cache::factory('Core', 'File', $fO, $bO);
Zend_Paginator::setCache($ca开发者_运维百科che);

$paginator->setItemCountPerPage(5);
$paginator->setCurrentPageNumber($page);

$this->view->paginator = $paginator;

Now every request, the paginator creates a neew cache entry and ignors the old one created? Any ideas?


I had some similar problems with Zend_Paginator. These problems only come with Zend_Paginator_Adapter_DbTableSelect (I had no problems with Zend_Paginator_Adapter_Array).

The bolded part of the cache filename zend_cache---Zend_Paginator_3_2b49905a9282f742e1cefafc53892794 is made by _getCacheId function (check Zend_Paginator) based on the adapter passed to the constructor. On each request, the paginator creates a new code because the adapter is never the same at the $_cache->load moment and $_cache->save moment.

The md5 serialized value of the adapter used as filename when data is saved into cache is different from the one used when data is read from cache if you have DB profiler enabled. You must not use DB profiler in order for the Zend_Paginator cache to work (use it only in development stage).

Another failure reason that I have found is determined by these two lines of code:

$offset = ($pageNumber - 1) * $this->getItemCountPerPage();
$items = $this->_adapter->getItems($offset, $this->getItemCountPerPage());

They are called between $_cache->load and $_cache->save and they add a limitcount value and a limitoffset value to adapter. This values aren't set when $_cache->load is called so the filename based on the md5 serialized value of the adapter will be different in this case too.

They must be placed before $_cache->load. You can make a paginator class that extends Zend_Paginator and modify getItemsByPage function. Add the two lines at the beggining, after $pageNumber = $this->normalizePageNumber($pageNumber).

This worked for me. I hope it will help others too.

0

精彩评论

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