Zend_Cache-Theory.xml 5.9 KB

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