CakePHP 使用Redis缓存Paginator的数据

本身的Paginator无法缓存直接修改源代码方便缓存 也可以另存为新的Component 本文上不了头条 管理员说文字描述不够 难道这年头作者都可以去写代码了是吧 简易的可以看github:http://christianyeah.github.io/CakePHP-Paginator-Cache/ /lib/Cake/Controller/Component/PaginatorComponent.php 定位到196行和大约215行
/*
修改一下代码
源代码仅为
$results = $object->find($type, array_merge($parameters, $extra));
*/
if(!isset($options['cache_config'])){
    $results = $object->find($type, array_merge($parameters, $extra));
}
else{
    $cache_key = 'paginator_cache_'.$object->name.'_page_'.$page;
    $results = Cache::remember($cache_key, function() use ($object,$type,$parameters,$extra){
                   return $object->find($type,array_merge($parameters, $extra));
               },$options['cache_config']);
}
 
 
//开始修改 以上代码不变 仅方便定位
/*
修改一下代码
源代码仅为
$results = $object->find($type, array_merge($parameters, $extra));
*/
if(!isset($options['cache_config'])){
    $count = $object->find('count', array_merge($parameters, $extra));
}
else{
    $cache_key = 'paginator_cache_'.$object->name.'_count';
    $count = Cache::remember($cache_key, function() use ($object,$type,$parameters,$extra){
                 return $object->find('count',array_merge($parameters, $extra));
             },$options['cache_config']);
}
使用方法: 首先修改core.php
//probability代表一个概率 查看源码后可以知道是time()%$probability ==0 时调用Cache::gc()
Cache::config('common_paginator_cache_redis', array(
    'engine' => 'Redis',
    'duration' => '+15 minutes',
    'probability' => 70
));
控制器中修改:
//使用方法
//加入以下代码设置cache config即可 其他代码无需更改
$this->paginate = array(
    'limit' => 12,
    'cache_config'=>'common_paginator_cache_redis'//新增 匹配core.php中的配置
);