Zend_Cache-Theory.xml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.cache.theory">
  4. <title>The Theory of Caching</title>
  5. <para>
  6. There are three key concepts in <classname>Zend_Cache</classname>. One is the unique
  7. identifier (a string) that is used to identify cache records. The second one is the
  8. <emphasis>'lifetime'</emphasis> directive as seen in the examples; it defines for how long
  9. the cached resource is considered 'fresh'. The third key concept is conditional execution so
  10. that parts of your code can be skipped entirely, boosting performance. The main frontend
  11. function (e.g. <methodname>Zend_Cache_Core::get()</methodname>) is always designed to return
  12. <constant>FALSE</constant> for a cache miss if that makes sense for the nature of a
  13. frontend. That enables
  14. end-users to wrap parts of the code they would like to cache (and skip) in
  15. <emphasis><methodname>if()</methodname>{ ... }</emphasis> statements where the condition is
  16. a <classname>Zend_Cache</classname> method itself. On the end if these blocks you must save
  17. what you've generated, however (e.g. <methodname>Zend_Cache_Core::save()</methodname>).
  18. </para>
  19. <note>
  20. <para>
  21. The conditional execution design of your generating code is not necessary in some
  22. frontends (<emphasis>Function</emphasis>, for an example) when the whole logic is
  23. implemented inside the frontend.
  24. </para>
  25. </note>
  26. <note>
  27. <para>
  28. 'Cache hit' is a term for a condition when a cache record is found, is valid and is
  29. 'fresh' (in other words hasn't expired yet). 'Cache miss' is everything else. When a
  30. cache miss happens, you must generate your data (as you would normally do) and have it
  31. cached. When you have a cache hit, on the other hand, the backend automatically fetches
  32. the record from cache transparently.
  33. </para>
  34. </note>
  35. <sect2 id="zend.cache.factory">
  36. <title>The Zend_Cache Factory Method</title>
  37. <para>
  38. A good way to build a usable instance of a <classname>Zend_Cache</classname> Frontend is
  39. given in the following example :
  40. </para>
  41. <programlisting language="php"><![CDATA[
  42. // We choose a backend (for example 'File' or 'Sqlite'...)
  43. $backendName = '[...]';
  44. // We choose a frontend (for example 'Core', 'Output', 'Page'...)
  45. $frontendName = '[...]';
  46. // We set an array of options for the chosen frontend
  47. $frontendOptions = array([...]);
  48. // We set an array of options for the chosen backend
  49. $backendOptions = array([...]);
  50. // We create an instance of Zend_Cache
  51. // (of course, the two last arguments are optional)
  52. $cache = Zend_Cache::factory($frontendName,
  53. $backendName,
  54. $frontendOptions,
  55. $backendOptions);
  56. ]]></programlisting>
  57. <para>
  58. In the following examples we will assume that the <varname>$cache</varname> variable
  59. holds a valid, instantiated frontend as shown and that you understand how to pass
  60. parameters to your chosen backends.
  61. </para>
  62. <note>
  63. <para>
  64. Always use <methodname>Zend_Cache::factory()</methodname> to get frontend instances.
  65. Instantiating frontends and backends yourself will not work as expected.
  66. </para>
  67. </note>
  68. </sect2>
  69. <sect2 id="zend.cache.tags">
  70. <title>Tagging Records</title>
  71. <para>
  72. Tags are a way to categorize cache records. When you save a cache with the
  73. <methodname>save()</methodname> method, you can set an array of tags to apply for this
  74. record. Then you will be able to clean all cache records tagged with a given tag (or
  75. tags):
  76. </para>
  77. <programlisting language="php"><![CDATA[
  78. $cache->save($huge_data, 'myUniqueID', array('tagA', 'tagB', 'tagC'));
  79. ]]></programlisting>
  80. <note>
  81. <para>
  82. note than the <methodname>save()</methodname> method accepts an optional fourth
  83. argument: <varname>$specificLifetime</varname> (if != <constant>FALSE</constant>,
  84. it sets a specific lifetime for this particular cache record)
  85. </para>
  86. </note>
  87. </sect2>
  88. <sect2 id="zend.cache.clean">
  89. <title>Cleaning the Cache</title>
  90. <para>
  91. To remove or invalidate in particular cache id, you can use the
  92. <methodname>remove()</methodname> method :
  93. </para>
  94. <programlisting language="php"><![CDATA[
  95. $cache->remove('idToRemove');
  96. ]]></programlisting>
  97. <para>
  98. To remove or invalidate several cache ids in one operation, you can use the
  99. <methodname>clean()</methodname> method. For example to remove all cache records :
  100. </para>
  101. <programlisting language="php"><![CDATA[
  102. // clean all records
  103. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  104. // clean only outdated
  105. $cache->clean(Zend_Cache::CLEANING_MODE_OLD);
  106. ]]></programlisting>
  107. <para>
  108. If you want to remove cache entries matching the tags 'tagA' and 'tagC':
  109. </para>
  110. <programlisting language="php"><![CDATA[
  111. $cache->clean(
  112. Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  113. array('tagA', 'tagC')
  114. );
  115. ]]></programlisting>
  116. <para>
  117. If you want to remove cache entries not matching the tags 'tagA' or 'tagC':
  118. </para>
  119. <programlisting language="php"><![CDATA[
  120. $cache->clean(
  121. Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
  122. array('tagA', 'tagC')
  123. );
  124. ]]></programlisting>
  125. <para>
  126. If you want to remove cache entries matching the tags 'tagA' or 'tagC':
  127. </para>
  128. <programlisting language="php"><![CDATA[
  129. $cache->clean(
  130. Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
  131. array('tagA', 'tagC')
  132. );
  133. ]]></programlisting>
  134. <para>
  135. Available cleaning modes are: <constant>CLEANING_MODE_ALL</constant>,
  136. <constant>CLEANING_MODE_OLD</constant>, <constant>CLEANING_MODE_MATCHING_TAG</constant>,
  137. <constant>CLEANING_MODE_NOT_MATCHING_TAG</constant> and
  138. <constant>CLEANING_MODE_MATCHING_ANY_TAG</constant>. The latter are, as their names
  139. suggest, combined with an array of tags in cleaning operations.
  140. </para>
  141. </sect2>
  142. </sect1>
  143. <!--
  144. vim:se ts=4 sw=4 et:
  145. -->