Caching Queries in CakePHP
Configure Cache in app/Config/bootstrap.php
cache file will be written in apptmpcachelong
In controller (ArticlesController)
‘order’=>’Article.created DESC’,
‘limit’=>$limit) ;
Cache::write(‘latest_news’, $data, ‘long’);
}
cache file will be named in apptmpcachelonglatest_news
In element
$latest_news = $this->requestAction(‘/Articles/latest_news/10’);
Cache::config(‘long’, array(
‘engine’ => ‘File’,
‘duration’ => ‘+1 hours’,
‘probability’ => 100,
‘path’ => CACHE . ‘long’ . DS,
));
cache file will be written in apptmpcachelong
In controller (ArticlesController)
public function latest_news ($limit=NULL) {
$data = Cache::read(‘latest_news’, ‘long’);
if (!$data) {
$data = array(‘fields’=>array(‘id’,’title’),
‘conditions’=>array(‘Article.published’=>1),
‘order’=>’Article.created DESC’,
‘limit’=>$limit) ;
Cache::write(‘latest_news’, $data, ‘long’);
}
return $this->Article->find(‘all’, $data);
}
cache file will be named in apptmpcachelonglatest_news
In element
$latest_news = $this->requestAction(‘/Articles/latest_news/10’);