How to control a cakephp cache duration
I’m using CakePHP1.2.5.
Cache is very powerful function for web pages using heavy SQL queries. If you use the CakePHP default Cache, you can see cache files in the app/tmp/cache directory.
I want to use the Cake Cache function at many points of my system and set different cache duration.
Firstly, check the core.php for using Cache.
//in core.php
Cache::config('default', array('engine' => 'File'));
This is a sample script using cache function, duration is 1 minute.
class PostController extends AppController {
var $uses = array('Post');
function index() {
$return_data = null;
Cache::set(array('duration' => '+60 seconds'));
if( ($return_data = Cache::read('PostController-index') ) === false ){
$return_data = $this->Post->find('all');
Cache::set(array('duration' => '+60 seconds'));
Cache::write('PostController-index' , $return_data);
}
$this->set('data',$return_data);
}
}
If there is no cache file(PostController-index),get DB data with the Post model and create cache file with its result. Array data is serialized.
You access same page within 1 minute, cake read cache file(PostController-index)
and return it insted of getting data from the Post model.
Key point is to use Cache::set() before each Cache::read and Cache::write(), we can change cache duration in each point.
http://book.cakephp.org/view/773/Cache-set
Be careful setting cache file name without overlap at each point. I recommend the rule, Class name + function name + parameter.
BTW, we can set Cache::config parameter in Cache::read() and Cache::write().
http://book.cakephp.org/view/766/Cache-read
http://book.cakephp.org/view/767/Cache-write
So, we can define multiple cache config as follow and use it each cache point.
//in core.php
Cache::config('default', array('engine' => 'File'));
Cache::config('onemin', array('engine' =>'File','duration'=> 60,));
Cache::config('onehour', array('engine' => 'File','duration'=> 3600,));
Cache::read('PostController-index', 'onemin')
Cache::write('PostController-index' , $return_data,'onemin');




October 8th, 2009 at 1:15 am
Seen my post: http://www.cake-toppings.com/2009/03/19/cache-duration-and-configuration-tips/ ?
October 8th, 2009 at 2:35 am
nice post, and @primeminster nice post, too.
one thing I wanted to throw out there was my approach to caching dynamically built queries. maybe you guys have some ideas or better ways to do this. say the query isn’t just a hardcoded find request, but pulls from some kind of user input… such as $result = $this->Post->find(’all’, array(’conditions’ => array(’Post.author_id’ => $author_id)));
Perhaps you could add a method to your model, or something in your controller that handles the following, but basically I’d take the parameters passed to find, serialize then md5 that value, and append it to your naming scheme. so PostController-index may become PostController-index-9e107d9d372bb6826bd81d3542a419d6 or the like.
October 8th, 2009 at 11:30 am
> primeminister
Thanks your comment and telling me your good article. I know your blog, but I didn’t know its article.
> pointlessjon
Thanks your comment.
It’s good way! serialize “find parameters” and md5 it to avoid cache name overlap.
May 3rd, 2010 at 4:49 pm
very well information you write it very clean. I’m very lucky to get this info from you.
August 17th, 2010 at 5:23 am
I like your style, the particular inescapable fact that your webpage is a little bit different tends to make it so useful, I get tired of discovering same-old-same-old almost all of the time. I have I just stumbled on this page by you Thank you.