如何CACHE查询/查找结果与PAGINATION CakePhp 2.x? [英] How to CACHE query/find results with PAGINATION CakePhp 2.x?
问题描述
我在CakePhp中使用 Cache :: read()
, Cache :: write()
I use Cache::read()
, Cache::write()
in CakePhp to cache queries.
如何更新paginator params或CACHE整个分页器?
缓存查询结果,但是如何缓存 $ this-> Paginator-> params()
?
It easy to cache query result but how to cache $this->Paginator->params()
too?
strong>我尝试过到 Cache :: write()
$ this-> Paginator-& code>但如何在缓存后修改
$ this-> Paginator-> params()
?当我做某事时:
...
I've tried to Cache::write()
$this->Paginator->params()
but how to modify $this->Paginator->params()
after caching? When I do someting like:
...
$this->Paginator->params['paging'][$model] = $paginator_params;
...
我收到通知:
... I've got notice:
Indirect modification of overloaded element of CakeRequest has no effect
我可以在哪里和何时重写PAGINATOR参数/设置缓存的参数?
谢谢。
推荐答案
您需要缓存 $ this-> request-> params ['paging']
array
You need to cache $this->request->params['paging']
array.
您必须将Paginator Helper添加到 $ helpers
数组。
And you have to add Paginator Helper to $helpers
array. If you don't, beforeRender method of Paginator helper won't be invoked and paging links will be broken.
$page = !isset($this->request->named['page']) ? '1' : $this->request->named['page'];
$products = Cache::read('my_unique_cache_id_' . $page, 'query');
$paging = Cache::read('my_unique_cache_id_paging_' . $page, 'query');
if ($products && $paging) {
$this->set('products', $products);
$this->request->params['paging'] = $paging;
}else{
$products = $this->paginate('Product');
Cache::write('my_unique_cache_id_paging_' . $page, $this->request->params['paging'], 'query');
Cache::write('my_unique_cache_id_' . $page, $products, 'query');
$this->set('products', $products);
}
这篇关于如何CACHE查询/查找结果与PAGINATION CakePhp 2.x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!