Zend_Cache-Theory.xml 6.2 KB

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