Zend_Cache-Introduction.xml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.cache.introduction">
  4. <title>Introduction</title>
  5. <para>
  6. <classname>Zend_Cache</classname> provides a generic way to cache any data.
  7. </para>
  8. <para>
  9. Caching in Zend Framework is operated by frontends while cache records are stored through
  10. backend adapters (<emphasis>File</emphasis>, <emphasis>Sqlite</emphasis>,
  11. <emphasis>Memcache</emphasis>...) through a flexible system of IDs and tags. Using those, it
  12. is easy to delete specific types of records afterwards (for example: "delete all cache records
  13. marked with a given tag").
  14. </para>
  15. <para>
  16. The core of the module (<classname>Zend_Cache_Core</classname>) is generic, flexible and
  17. configurable. Yet, for your specific needs there are cache frontends that extend
  18. <classname>Zend_Cache_Core</classname> for convenience: <emphasis>Output</emphasis>,
  19. <emphasis>File</emphasis>, <emphasis>Function</emphasis> and <emphasis>Class</emphasis>.
  20. </para>
  21. <example id="zend.cache.introduction.example-1">
  22. <title>Getting a Frontend with Zend_Cache::factory()</title>
  23. <para>
  24. <methodname>Zend_Cache::factory()</methodname> instantiates correct objects and ties
  25. them together. In this first example, we will use <emphasis>Core</emphasis> frontend
  26. together with <emphasis>File</emphasis> backend.
  27. </para>
  28. <programlisting language="php"><![CDATA[
  29. $frontendOptions = array(
  30. 'lifetime' => 7200, // cache lifetime of 2 hours
  31. 'automatic_serialization' => true
  32. );
  33. $backendOptions = array(
  34. 'cache_dir' => './tmp/' // Directory where to put the cache files
  35. );
  36. // getting a Zend_Cache_Core object
  37. $cache = Zend_Cache::factory('Core',
  38. 'File',
  39. $frontendOptions,
  40. $backendOptions);
  41. ]]></programlisting>
  42. </example>
  43. <note>
  44. <title>Frontends and Backends Consisting of Multiple Words</title>
  45. <para>
  46. Some frontends and backends are named using multiple words, such
  47. as 'ZendPlatform'. When specifying them to the factory, separate
  48. them using a word separator, such as a space (' '), hyphen
  49. ('-'), or period ('.').
  50. </para>
  51. </note>
  52. <example id="zend.cache.introduction.example-2">
  53. <title>Caching a Database Query Result</title>
  54. <para>
  55. Now that we have a frontend, we can cache any type of data (we turned on serialization).
  56. for example, we can cache a result from a very expensive database query. After it is
  57. cached, there is no need to even connect to the database; records are fetched from cache
  58. and unserialized.
  59. </para>
  60. <programlisting language="php"><![CDATA[
  61. // $cache initialized in previous example
  62. // see if a cache already exists:
  63. if( ($result = $cache->load('myresult')) === false ) {
  64. // cache miss; connect to the database
  65. $db = Zend_Db::factory( [...] );
  66. $result = $db->fetchAll('SELECT * FROM huge_table');
  67. $cache->save($result, 'myresult');
  68. } else {
  69. // cache hit! shout so that we know
  70. echo "This one is from cache!\n\n";
  71. }
  72. print_r($result);
  73. ]]></programlisting>
  74. </example>
  75. <example id="zend.cache.introduction.example-3">
  76. <title>Caching Output with Zend_Cache Output Frontend</title>
  77. <para>
  78. We 'mark up' sections in which we want to cache output by adding some conditional logic,
  79. encapsulating the section within <methodname>start()</methodname> and
  80. <methodname>end()</methodname> methods (this resembles the first example and is the core
  81. strategy for caching).
  82. </para>
  83. <para>
  84. Inside, output your data as usual - all output will be cached when execution hits the
  85. <methodname>end()</methodname> method. On the next run, the whole section will be
  86. skipped in favor of fetching data from cache (as long as the cache record is valid).
  87. </para>
  88. <programlisting language="php"><![CDATA[
  89. $frontendOptions = array(
  90. 'lifetime' => 30, // cache lifetime of 30 seconds
  91. 'automatic_serialization' => false // this is the default anyways
  92. );
  93. $backendOptions = array('cache_dir' => './tmp/');
  94. $cache = Zend_Cache::factory('Output',
  95. 'File',
  96. $frontendOptions,
  97. $backendOptions);
  98. // we pass a unique identifier to the start() method
  99. if(!$cache->start('mypage')) {
  100. // output as usual:
  101. echo 'Hello world! ';
  102. echo 'This is cached ('.time().') ';
  103. $cache->end(); // the output is saved and sent to the browser
  104. }
  105. echo 'This is never cached ('.time().').';
  106. ]]></programlisting>
  107. <para>
  108. Notice that we output the result of <methodname>time()</methodname> twice; this is
  109. something dynamic for demonstration purposes. Try running this and then refreshing
  110. several times; you will notice that the first number doesn't change while second changes
  111. as time passes. That is because the first number was output in the cached section and is
  112. saved among other output. After half a minute (we've set lifetime to 30 seconds) the
  113. numbers should match again because the cache record expired -- only to be cached again.
  114. You should try this in your browser or console.
  115. </para>
  116. </example>
  117. <note>
  118. <para>
  119. When using <classname>Zend_Cache</classname>, pay attention to the important cache
  120. identifier (passed to <methodname>save()</methodname> and
  121. <methodname>start()</methodname>). It must be unique for every resource you cache,
  122. otherwise unrelated cache records may wipe each other or, even worse, be displayed in
  123. place of the other.
  124. </para>
  125. </note>
  126. </sect1>
  127. <!--
  128. vim:se ts=4 sw=4 et:
  129. -->