CakePHP 2分页缓存

时间:2016-02-25 08:23:28

标签: cakephp caching pagination

问题在于products表中的许多记录,并且每个客户都有自己的产品价格(product_prices)。

产品是客户管理的分页订购每页显示

我根据here

编写了自己的自定义分页

但是当客户端更改排序每页显示时,新创建的缓存和缓存的这个想法并不好。

我正在寻找能够以很多价格缓存许多产品的最佳解决方案。

1 个答案:

答案 0 :(得分:0)

您可以按页面缓存产品,而不是手动分配分页设置:

$products_all_count = ???// you must count all products that match your conditions
$curr_page = (!empty($this->request->query('page')))? (int)$this->request->query('page') : 1;
$products_cache_key = 'product_' $products_all_count . '_page_' . $curr_page;
$products = Cache::read($products_cache_key);

$products_sett = array();
$products_sett['page'] = $curr_page;            
$products_sett['limit'] = 100; // defined limit per page


if (!$products) {

 // all conditions, settings etc.
 $products = $this->Paginator->paginate('Product');
 Cache::write($products_cache_key, $products);

} elseif (is_array($products)) {

 $this->request->params['paging'] = array(
    'Product' => array(
        'current' =>  $products_sett['limit'] * $products_sett['page'],
        'count' => $products_all_count,
        'prevPage' => (($products_sett['limit'] * $products_sett['page']) <= $products_sett['limit'])? false : true,
        'nextPage' => (($products_sett['limit'] * $products_sett['page']) >= $products_all_count)? false : true,
        'pageCount' => ceil($products_all_count / $products_sett['limit']),
        'limit' => $products_sett['limit'] ,
        'page' => $products_sett['page'],
        'options' => array()
    )
 );

}