Zend_Cache-Cache_Manager.xml 6.8 KB

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