2008-06-03

CachedObjects, a CodeIgniter extension

Im using CodeIgniter in a small pet-project in my spare time and missed a more granular cache than the entire page, and the code
$cache = get_cache('that_snippet',60);
if (not $cache) {
  // slow code here;
  $cache = $result_of_slow_code;
  write_cache('that_snippet',$cache);
}
return $cache;
is too ugly to be duplicated all over the code.
So using a little PHP hackerism (object __call() method) and extending the controller and model classes of CodeIgniter, now I can cache the methods results of controllers and models. All that you need to get cached results of myslowmethod(parameters) is call myslowmethodCached(parameters). Clean and Elegant to use, based on a ugly php hackerism, isolated in one place. http://sergio.bruder.com.br/cachedobjects/