Skip to content

Caching with Zend_Cache

2009 December 21
by Richard Knop

Caching with PHP is the best way to speed up your application and it’s quite easy thanks to native PHP modules such as APC or output buffering control. It’s even much much easier when you are using the Zend Framework as it contains a native class just for caching – Zend_Cache. To use the Zend_Cache you must first initialize it in the bootstrap and add it to the registry so you can access it easily in controllers, I do it like this:

  1. protected function _initCache()
  2. {
  3.     $frontend = array('lifetime' => 7200,
  4.                      'automatic_seralization' => true);
  5.     // where are we going to stored the cached files?
  6.     $backend = array('cache_dir' => 'cache');
  7.     $this->cache = Zend_Cache::factory('core',
  8.                                        'File',
  9.                                        $frontend,
  10.                                        $backend);
  11. }
  12.  
  13. protected function _initRegistry()
  14. {
  15.     $this->registry = Zend_Registry::getInstance();
  16.     $this->registry->cache = $this->cache;
  17.     // I store much more in the registry, of course
  18.     // for instance, configuration and db adapter
  19. }

The frontend and the backend adapters can take much more arguments, read the documentation to learn all possible options. I initialized the Zend_Cache object with the factory method. First argument means that we want to use the Zend_Cache_Core frontend which is the core of the Zend_Cache module. The second argument is the backend we want to use the Zend_Cache_Backend_File backend or that we want to store cached data as files in a specific directory. The other two parameters are frontend and backend options.

Now we can easily access the cache object in controllers or models:

  1. $cache = Zend_Registry::get('cache');
  2. // does the cache contain data we are looking for?
  3. if (!$result = $cache->load('myUniqueId')) {
  4.     // if not let's cache the data
  5.     // here I use only a random array but in a real application
  6.     // you would probably cache some database entries
  7.     $data = array('John Doe', 'Jane Doe', 'Baby Doe');
  8.     // besides unique cache id you can use tags to categorize data
  9.     $cache->save($data, 'myUniqueId', array('tag1', 'tag2'));
  10. } else {
  11.     // dump the cached data
  12.     var_dump($result);
  13. }

That was easy, wasn’t it?

Cleaning the cache

You will surely also want to remove cached files at some point in your application:

  1. // clean all cached files
  2. $cache->clean(Zend_Cache:: CLEANING_MODE_ALL);
  3. // clean only outdated cached files
  4. $cache->clean(Zend_Cache::CLEANING_MODE_OLD);
  5. // remove a particular cache id
  6. $cache->remove('myUniqueId');
  7. // remove records tagged as "tag1" AND "tag2"
  8. $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  9.               array('tag1', 'tag2'));
  10. // remove records tagged as "tag1" OR :tag2"
  11. $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
  12.               array('tag1', 'tag2'));
  13. // remove records NOT tagged as "tag1" or "tag2"
  14. $cache->clean(Zend_Cache::CLEANING_MODE_NOT_MATCHING_ANY_TAG,
  15.               array('tag1', 'tag2'));

That’s it for today and, by the way, Merry Christmas :)

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS