The Cache Manager

It's the nature of applications to require a multitude of caches of any type often dependent on the controller, library or domain model being accessed. To allow for a simple means of defining Zend_Cache options in advance (such as from a bootstrap) so that accessing a cache object requires minimum setup within the application source code, the Zend_Cache_Manager class was written. This class is accompanied by Zend_Application_Resource_Cachemanager ensuring bootstrap configuration is available and Zend_Controller_Action_Helper_Cache to allow simple cache access and instantiation from controllers and other helpers.

The basic operation of this component is as follows. The Cache Manager allows users to setup "option templates", basically options for a set of named caches. These can be set using the method Zend_Cache_Manager::setCacheTemplate(). These templates do not give rise to a cache until the user attempts to retrieve a named cache (associated with an existing option template) using the method Zend_Cache_Manager::getCache().

  1. $manager = new Zend_Cache_Manager;
  2.  
  3. $dbCache = array(
  4.     'frontend' => array(
  5.         'name' => 'Core',
  6.         'options' => array(
  7.             'lifetime' => 7200,
  8.             'automatic_serialization' => true
  9.         )
  10.     ),
  11.     'backend' => array(
  12.         'name' => 'Core',
  13.         'options' => array(
  14.             'cache_dir' => '/path/to/cache'
  15.         )
  16.     )
  17. );
  18.  
  19. $manager->setCacheTemplate('database', $dbCache);
  20.  
  21. /**
  22. * Anywhere else where the Cache Manager is available...
  23. */
  24. $databaseCache = $manager->getCache('database');

The Cache Manager also allows simple setting of pre-instantiated caches using the method Zend_Cache_Manager::setCache().

  1. $frontendOptions = array(
  2.    'lifetime' => 7200,
  3.    'automatic_serialization' => true
  4. );
  5.  
  6. $backendOptions = array(
  7.     'cache_dir' => '/path/to/cache'
  8. );
  9.  
  10. $dbCache = Zend_Cache::factory('Core',
  11.                              'File',
  12.                              $frontendOptions,
  13.                              $backendOptions);
  14.  
  15. $manager = new Zend_Cache_Manager;
  16. $manager->setCache('database', $dbCache);
  17.  
  18. /**
  19. * Anywhere else where the Cache Manager is available...
  20. */
  21. $databaseCache = $manager->getCache('database');

If for any reason, you are unsure where the Cache Manager contains a pre-instantiated cache or a relevant option cache template to create one on request, you can check for the existence of a name cache configuration or instance using the method Zend_Cache_Manager::hasCache().

  1. $manager = new Zend_Cache_Manager;
  2.  
  3. $dbCache = array(
  4.     'frontend' => array(
  5.         'name' => 'Core',
  6.         'options' => array(
  7.             'lifetime' => 7200,
  8.             'automatic_serialization' => true
  9.         )
  10.     ),
  11.     'backend' => array(
  12.         'name' => 'Core',
  13.         'options' => array(
  14.             'cache_dir' => '/path/to/cache'
  15.         )
  16.     )
  17. );
  18.  
  19. $manager->setCacheTemplate('database', $dbCache);
  20.  
  21. /**
  22. * Anywhere else where the Cache Manager is available...
  23. */
  24. if ($manager->hasCache('database')) {
  25.     $databaseCache = $manager->getCache('database');
  26. } else {
  27.     // create a cache from scratch if none available from Manager
  28. }

In some scenarios, you may have defined a number of general use caches using Zend_Cache_Manager but need to fine-tune their options before use depending on the circumstances. You can edit previously set cache templates on the fly before they are instantiated using the method Zend_Cache_Manager::setTemplateOptions().

  1. $manager = new Zend_Cache_Manager;
  2.  
  3. $dbCache = array(
  4.     'frontend' => array(
  5.         'name' => 'Core',
  6.         'options' => array(
  7.             'lifetime' => 7200,
  8.             'automatic_serialization' => true
  9.         )
  10.     ),
  11.     'backend' => array(
  12.         'name' => 'Core',
  13.         'options' => array(
  14.             'cache_dir' => '/path/to/cache'
  15.         )
  16.     )
  17. );
  18.  
  19. $manager->setCacheTemplate('database', $dbCache);
  20.  
  21. /**
  22. * Anywhere else where the Cache Manager is available...
  23. * Here we decided to store some upcoming database queries to Memcached instead
  24. * of the preconfigured File backend.
  25. */
  26. $fineTuning = array(
  27.     'backend' => array(
  28.         'name' => 'Memcached',
  29.         'options' => array(
  30.             'servers' => array(
  31.                 array(
  32.                     'host' => 'localhost',
  33.                     'port' => 11211,
  34.                     'persistent' => true,
  35.                     'weight' => 1,
  36.                     'timeout' => 5,
  37.                     'retry_interval' => 15,
  38.                     'status' => true,
  39.                     'failure_callback' => ''
  40.                 )
  41.             )
  42.         )
  43.     )
  44. );
  45. $manager->setTemplateOptions('database', $fineTuning);
  46. $databaseCache = $manager->getCache('database');

To assist in making the Cache Manager more useful, it is accompanied by Zend_Application_Resource_Cachemanager and also the Zend_Controller_Action_Helper_Cache Action Helper. Both of these are described in their relevant areas of the Reference Guide.

Out of the box, Zend_Cache_Manager already includes four pre-defined cache templates called "skeleton", "default", "page" and "tagcache". The default cache is a simple File based cache using the Core frontend which assumes a cache_dir called "cache" exists at the same level as the conventional "public" directory of a Zend Framework application. The skeleton cache is actually a NULL cache, i.e. it contains no options. The remaining two caches are used to implement a default Static Page Cache where static HTML, XML or even JSON may be written to static files in /public. Control over a Static Page Cache is offered via Zend_Controller_Action_Helper_Cache, though you may alter the settings of this "page" the "tagcache" it uses to track tags using Zend_Cache_Manager::setTemplateOptions() or even Zend_Cache_Manager::setCacheTemplate() if overloading all of their options.

blog comments powered by Disqus