Zend_cache overview

Zend_Cache is a versatile generic Zend Framework component to manage cached data.
The cached objects are saved and retrieved with an ID (string) or a list of tags.

The supported Backends (how the cache is saved and retrieved) are:

  • File:
    Cache are saved on disk (using
    Options: cache_dir, file_locking, hashed_directory_level
  • SqliteMemcached, Apc, Xcache, TwoLevels, Static

The supported FrontEnds (how the cache is used on site elements) are:

  • Core:
    The most used, to save PHP objects/variables. It provides the methods (all of them accept the cache_id as a first argument): loadsavetest (check if the cache is valid), remove.
    Options: lifetime (seconds), automatic_serialization (boolean),

    if( !$result = $cache->load('myresult') ) {
      $result  = loadResultFromDB();  /* fill $result, for example with DB data */
      $cache->save($result, 'myresult');
    /* use $result */
  • Output
    It uses start and end methods to start/end caching the content written.
  • File, FunctionClasses


To initialize the cache, there is a static method (factory Design Pattern) that accepts backend method name, frontend method name and their respective options (arrays key and values).

$cache = Zend_Cache::factory('Core', /*frontend name*/
'File', /* backend method name*/

/* save to registry*/
Zend_Registry::set('cache', $cache);

/* USING: get from registry*/
$cache = Zend_Registry::get('cache');
 //  ...use...

How to cache special values (equivalent to boolean “false”, like empty strings)

if( $cache->test('myresult') ) {
   $result = $cache->load('myresult');
} else {
  $result  = loadResultFromDB();  /* fill $result, for example with DB data */
  $cache->save($result, 'myresult');

/* use $result */

At this address, a class to manage zend cache by Rob Allen