Here is a small code on how to save or cache stuff, meaning object, array, class, almost anything except view (you can refer to the CakePHP 1.1 document on how to cache views for it still work on cake 1.2, and retrive or get back the cached/saved stuff back.

The following tutorial will use File as Cache Engine and duration is set to 3600000 in the core.php
Cache::config('default', array('engine' => 'File','duration'=> 3600000));
Reason behind why we set it to 3600000 is that if you look at the codes on how it had been implemented, it checks the default duration added the current time and compared to the cache time you specify and if the cache time is greater then the first one then it will not return anything (it spend me hours to find this one out), you can refer to the codes on file.php, line 171 on the folder cake/libs/cache.

Back to the how to save stuff and retrieve it later
function saveArtistData($artistId){
$cacheName = "artist_datas_".$artistId;
$data = $this->Artist->findAll();
Cache::write($cacheName,$data,'+1 weeks');
}
function getArtistData($artistId){
$cacheName = "artist_datas_".$artistId;
return Cache::read($cacheName);
}

Pretty simple? Just use Cache::write to write data (The API here), and Cache::read to read data (API). The data by default will be serialize and by default will be place under app/tmp/cache

Hope this helps

Share and Enjoy:
  • Digg
  • Reddit
  • Slashdot
  • del.icio.us
  • StumbleUpon
  • TwitThis
  • Fark
  • Facebook
  • Technorati
  • Sphinn
  • Furl
  • Google
  • Mixx