Zend_Cache-Cache_Manager.xml 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 20057 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.cache.cache.manager">
  5. <title>Der Cache Manager</title>
  6. <para>
  7. It's the nature of applications to require a multitude of caches of any
  8. type often dependent on the controller, library or domain model being
  9. accessed. To allow for a simple means of defining <classname>Zend_Cache</classname>
  10. options in advance (such as from a bootstrap) so that accessing a cache
  11. object requires minimum setup within the application source code, the
  12. <classname>Zend_Cache_Manager</classname> class was written. This class
  13. is accompanied by <classname>Zend_Application_Resource_Cachemanager</classname>
  14. ensuring bootstrap configuration is available and
  15. <classname>Zend_Controller_Action_Helper_Cache</classname> to allow simple
  16. cache access and instantiation from controllers and other helpers.
  17. </para>
  18. <para>
  19. The basic operation of this component is as follows. The Cache Manager allows
  20. users to setup "option templates", basically options for a set of named
  21. caches. These can be set using the method <methodname>Zend_Cache_Manager::setCacheTemplate()</methodname>.
  22. These templates do not give rise to a cache until the user attempts to retrieve
  23. a named cache (associated with an existing option template) using the method
  24. <methodname>Zend_Cache_Manager::getCache()</methodname>.
  25. </para>
  26. <programlisting language="php"><![CDATA[
  27. $manager = new Zend_Cache_Manager;
  28. $dbCache = array(
  29. 'frontend' => array(
  30. 'name' => 'Core',
  31. 'options' => array(
  32. 'lifetime' => 7200,
  33. 'automatic_serialization' => true
  34. )
  35. ),
  36. 'backend' => array(
  37. 'name' => 'Core',
  38. 'options' => array(
  39. 'cache_dir' => '/path/to/cache'
  40. )
  41. )
  42. );
  43. $manager->setCacheTemplate('database', $dbCache);
  44. /**
  45. * Anywhere else where the Cache Manager is available...
  46. */
  47. $databaseCache = $manager->getCache('database');
  48. ]]></programlisting>
  49. <para>
  50. The Cache Manager also allows simple setting of pre-instantiated caches
  51. using the method <methodname>Zend_Cache_Manager::setCache()</methodname>.
  52. </para>
  53. <programlisting language="php"><![CDATA[
  54. $frontendOptions = array(
  55. 'lifetime' => 7200,
  56. 'automatic_serialization' => true
  57. );
  58. $backendOptions = array(
  59. 'cache_dir' => '/path/to/cache'
  60. );
  61. $dbCache = Zend_Cache::factory('Core',
  62. 'File',
  63. $frontendOptions,
  64. $backendOptions);
  65. $manager = new Zend_Cache_Manager;
  66. $manager->setCache('database', $dbCache);
  67. /**
  68. * Anywhere else where the Cache Manager is available...
  69. */
  70. $databaseCache = $manager->getCache('database');
  71. ]]></programlisting>
  72. <para>
  73. If for any reason, you are unsure where the Cache Manager contains a
  74. pre-instantiated cache or a relevant option cache template to create one
  75. on request, you can check for the existance of a name cache configuration
  76. or instance using the method <methodname>Zend_Cache_Manager::hasCache()</methodname>.
  77. </para>
  78. <programlisting language="php"><![CDATA[
  79. $manager = new Zend_Cache_Manager;
  80. $dbCache = array(
  81. 'frontend' => array(
  82. 'name' => 'Core',
  83. 'options' => array(
  84. 'lifetime' => 7200,
  85. 'automatic_serialization' => true
  86. )
  87. ),
  88. 'backend' => array(
  89. 'name' => 'Core',
  90. 'options' => array(
  91. 'cache_dir' => '/path/to/cache'
  92. )
  93. )
  94. );
  95. $manager->setCacheTemplate('database', $dbCache);
  96. /**
  97. * Anywhere else where the Cache Manager is available...
  98. */
  99. if ($manager->hasCache('database')) {
  100. $databaseCache = $manager->getCache('database');
  101. } else {
  102. // create a cache from scratch if none available from Manager
  103. }
  104. ]]></programlisting>
  105. <para>
  106. In some scenarios, you may have defined a number of general use caches
  107. using <classname>Zend_Cache_Manager</classname> but need to fine-tune
  108. their options before use depending on the circumstances. You can edit
  109. previously set cache templates on the fly before they are instantiated
  110. using the method <methodname>Zend_Cache_Manager::setTemplateOptions()</methodname>.
  111. </para>
  112. <programlisting language="php"><![CDATA[
  113. $manager = new Zend_Cache_Manager;
  114. $dbCache = array(
  115. 'frontend' => array(
  116. 'name' => 'Core',
  117. 'options' => array(
  118. 'lifetime' => 7200,
  119. 'automatic_serialization' => true
  120. )
  121. ),
  122. 'backend' => array(
  123. 'name' => 'Core',
  124. 'options' => array(
  125. 'cache_dir' => '/path/to/cache'
  126. )
  127. )
  128. );
  129. $manager->setCacheTemplate('database', $dbCache);
  130. /**
  131. * Anywhere else where the Cache Manager is available...
  132. * Here we decided to store some upcoming database queries to Memcached instead
  133. * of the preconfigured File backend.
  134. */
  135. $fineTuning = array(
  136. 'backend' => array(
  137. 'name' => 'Memcached',
  138. 'options' => array(
  139. 'servers' => array(
  140. array(
  141. 'host' => 'localhost',
  142. 'port' => 11211,
  143. 'persistent' => true,
  144. 'weight' => 1,
  145. 'timeout' => 5,
  146. 'retry_interval' => 15,
  147. 'status' => true,
  148. 'failure_callback' => ''
  149. )
  150. )
  151. )
  152. )
  153. );
  154. $manager->setTemplateOptions('database', $fineTuning);
  155. $databaseCache = $manager->getCache('database');
  156. ]]></programlisting>
  157. <para>
  158. To assist in making the Cache Manager more useful, it is accompanied by
  159. <classname>Zend_Application_Resource_Cachemanager</classname> and also
  160. the <classname>Zend_Controller_Action_Helper_Cache</classname> Action
  161. Helper. Both of these are described in their relevant areas of the
  162. Reference Guide.
  163. </para>
  164. <para>
  165. Out of the box, <classname>Zend_Cache_Manager</classname> already includes
  166. four pre-defined cache templates called "skeleton", "default", "page" and
  167. "tagcache". The default cache is a simple File based cache using the Core
  168. frontend which assumes a cache_dir called "cache" exists at the same
  169. level as the conventional "public" directory of a Zend Framework application.
  170. The skeleton cache is actually a NULL cache, i.e. it contains no options.
  171. The remaining two caches are used to implement a default Static Page Cache
  172. where static HTML/XML or even JSON may be written to static files in
  173. /public. Control over a Static Page Cache is offered via
  174. <classname>Zend_Controller_Action_Helper_Cache</classname>, though you may
  175. alter the settings of this "page" the "tagcache" it uses to track tags using
  176. <methodname>Zend_Cache_Manager::setTemplateOptions()</methodname> or even
  177. <methodname>Zend_Cache_Manager::setCacheTemplate()</methodname> if overloading
  178. all of their options.
  179. </para>
  180. </sect1>