Zend_Cache-Introduction.xml 5.8 KB

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