Zend_Cache-Theory.xml 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 (eg. <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 (eg. <methodname>Zend_Cache_Core::save()</methodname>).
  18. </para>
  19. <note><para>
  20. The conditional execution design of your generating code is not necessary in some frontends
  21. (<emphasis>Function</emphasis>, for an example) when the whole logic is implemented inside
  22. the frontend.
  23. </para></note>
  24. <note><para>
  25. 'Cache hit' is a term for a condition when a cache record is found, is valid and is 'fresh'
  26. (in other words hasn't expired yet). 'Cache miss' is everything else. When a cache miss
  27. happens, you must generate your data (as you would normally do) and have it cached. When you
  28. have a cache hit, on the other hand, the backend automatically fetches the record from cache
  29. transparently.
  30. </para></note>
  31. <sect2 id="zend.cache.factory">
  32. <title>The Zend_Cache Factory Method</title>
  33. <para>
  34. A good way to build a usable instance of a <classname>Zend_Cache</classname> Frontend is
  35. given in the following example :
  36. </para>
  37. <programlisting language="php"><![CDATA[
  38. // We choose a backend (for example 'File' or 'Sqlite'...)
  39. $backendName = '[...]';
  40. // We choose a frontend (for example 'Core', 'Output', 'Page'...)
  41. $frontendName = '[...]';
  42. // We set an array of options for the choosen frontend
  43. $frontendOptions = array([...]);
  44. // We set an array of options for the choosen backend
  45. $backendOptions = array([...]);
  46. // We create an instance of Zend_Cache
  47. // (of course, the two last arguments are optional)
  48. $cache = Zend_Cache::factory($frontendName,
  49. $backendName,
  50. $frontendOptions,
  51. $backendOptions);
  52. ]]></programlisting>
  53. <para>
  54. In the following examples we will assume that the <varname>$cache</varname> variable
  55. holds a valid, instantiated frontend as shown and that you understand how to pass
  56. parameters to your chosen backends.
  57. </para>
  58. <note><para>
  59. Always use <methodname>Zend_Cache::factory()</methodname> to get frontend instances.
  60. Instantiating frontends and backends yourself will not work as expected.
  61. </para></note>
  62. </sect2>
  63. <sect2 id="zend.cache.tags">
  64. <title>Tagging Records</title>
  65. <para>
  66. Tags are a way to categorize cache records. When you save a cache with the
  67. <methodname>save()</methodname> method, you can set an array of tags to apply for this
  68. record. Then you will be able to clean all cache records tagged with a given tag (or
  69. tags):
  70. </para>
  71. <programlisting language="php"><![CDATA[
  72. $cache->save($huge_data, 'myUniqueID', array('tagA', 'tagB', 'tagC'));
  73. ]]></programlisting>
  74. <note><para>
  75. note than the <methodname>save()</methodname> method accepts an optional fourth
  76. argument: <varname>$specificLifetime</varname> (if != <constant>FALSE</constant>,
  77. it sets a specific lifetime for this particular cache record)
  78. </para></note>
  79. </sect2>
  80. <sect2 id="zend.cache.clean">
  81. <title>Cleaning the Cache</title>
  82. <para>
  83. To remove or invalidate in particular cache id, you can use the
  84. <methodname>remove()</methodname> method :
  85. </para>
  86. <programlisting language="php"><![CDATA[
  87. $cache->remove('idToRemove');
  88. ]]></programlisting>
  89. <para>
  90. To remove or invalidate several cache ids in one operation, you can use the
  91. <methodname>clean()</methodname> method. For example to remove all cache records :
  92. </para>
  93. <programlisting language="php"><![CDATA[
  94. // clean all records
  95. $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
  96. // clean only outdated
  97. $cache->clean(Zend_Cache::CLEANING_MODE_OLD);
  98. ]]></programlisting>
  99. <para>
  100. If you want to remove cache entries matching the tags 'tagA' and 'tagC':
  101. </para>
  102. <programlisting language="php"><![CDATA[
  103. $cache->clean(
  104. Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  105. array('tagA', 'tagC')
  106. );
  107. ]]></programlisting>
  108. <para>
  109. If you want to remove cache entries not matching the tags 'tagA' or 'tagC':
  110. </para>
  111. <programlisting language="php"><![CDATA[
  112. $cache->clean(
  113. Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
  114. array('tagA', 'tagC')
  115. );
  116. ]]></programlisting>
  117. <para>
  118. If you want to remove cache entries matching the tags 'tagA' or 'tagC':
  119. </para>
  120. <programlisting language="php"><![CDATA[
  121. $cache->clean(
  122. Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
  123. array('tagA', 'tagC')
  124. );
  125. ]]></programlisting>
  126. <para>
  127. Available cleaning modes are: <constant>CLEANING_MODE_ALL</constant>,
  128. <constant>CLEANING_MODE_OLD</constant>, <constant>CLEANING_MODE_MATCHING_TAG</constant>,
  129. <constant>CLEANING_MODE_NOT_MATCHING_TAG</constant> and
  130. <constant>CLEANING_MODE_MATCHING_ANY_TAG</constant>. The latter are, as their names
  131. suggest, combined with an array of tags in cleaning operations.
  132. </para>
  133. </sect2>
  134. </sect1>
  135. <!--
  136. vim:se ts=4 sw=4 et:
  137. -->