Zend_Cache-Frontends.xml 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. <sect1 id="zend.cache.frontends">
  2. <title>Zend_Cache前端</title>
  3. <sect2 id="zend.cache.frontends.core">
  4. <title>Zend_Cache_Core</title>
  5. <sect3 id="zend.cache.frontends.core.introduction">
  6. <title>简介</title>
  7. <para>
  8. <code>Zend_Cache_Core</code>是一个特别的前端,因为他是模块的核心. 它是一个一般化(generic)的缓存前端,并且由其他类扩展.
  9. </para>
  10. <note><para>
  11. 所有的前端继承自<code>Zend_Cache_Core</code>因此它的方法和选项(描述如下)应该在其他的前端中可用,所以这里没有进行文档化.
  12. </para></note>
  13. </sect3>
  14. <sect3 id="zend.cache.frontends.core.options">
  15. <title>可用选项</title>
  16. <para>
  17. 这些选项被传递给如前面例子中演示的工厂方法.
  18. </para>
  19. <table id="zend.cache.frontends.core.options.table">
  20. <title>核心前端选项</title>
  21. <tgroup cols="4">
  22. <thead>
  23. <row>
  24. <entry>选项</entry>
  25. <entry>数据类型</entry>
  26. <entry>默认值</entry>
  27. <entry>描述</entry>
  28. </row>
  29. </thead>
  30. <tbody>
  31. <row>
  32. <entry><code>caching</code></entry>
  33. <entry><code>boolean</code></entry>
  34. <entry><code>true</code></entry>
  35. <entry>
  36. 打开 / 关闭缓存 (对被缓存脚本的调试非常有用)
  37. </entry>
  38. </row>
  39. <row>
  40. <entry><code>cache_id_prefix</code></entry>
  41. <entry><code>string</code></entry>
  42. <entry><code>null</code></entry>
  43. <entry>
  44. 所有缓存 id 的前缀,如果设置为 <code>null</code> ,没有缓存 id 前缀使用。 缓存 id 前缀在缓存里创建一个命名空间,允许多个程序和网上共享缓存。 每个程序或网站可以使用不同的缓存 id 前缀,所以特定的缓存 id 可以使用多次。
  45. </entry>
  46. </row>
  47. <row>
  48. <entry><code>lifetime</code></entry>
  49. <entry><code>int</code></entry>
  50. <entry><code>3600</code></entry>
  51. <entry>
  52. 缓存生命期(秒), 如果设置为 <code>null</code>, 缓存永远有效.
  53. </entry>
  54. </row>
  55. <row>
  56. <entry><code>logging</code></entry>
  57. <entry><code>boolean</code></entry>
  58. <entry><code>false</code></entry>
  59. <entry>
  60. 如果设置为true,日志纪录(通过使用<code>Zend_Log</code>)被激活(但是系统将变慢)
  61. </entry>
  62. </row>
  63. <row>
  64. <entry><code>write_control</code></entry>
  65. <entry><code>boolean</code></entry>
  66. <entry><code>true</code></entry>
  67. <entry>
  68. 打开 / 关闭 写控制 (the cache is read just after writing to detect corrupt entries),打开写控制轻微地放慢缓存写的速度但不影响读(it can detect some corrupt cache files but it's not a perfect control)
  69. </entry>
  70. </row>
  71. <row>
  72. <entry><code>automatic_serialization</code></entry>
  73. <entry><code>boolean</code></entry>
  74. <entry><code>false</code></entry>
  75. <entry>
  76. 打开 / 关闭自动序列化, 可以直接用于保存非字符串数据(但是很慢)
  77. </entry>
  78. </row>
  79. <row>
  80. <entry><code>automatic_cleaning_factor</code></entry>
  81. <entry><code>int</code></entry>
  82. <entry><code>10</code></entry>
  83. <entry>
  84. 关闭 / 调整自动清理过程 (垃圾收集器):
  85. 0 表示不自动清理缓存,1 表示自动清理缓存,并且如果x &gt; 1 表示x写操作后自动随机清理1次.
  86. </entry>
  87. </row>
  88. <row>
  89. <entry><code>ignore_user_abort</code></entry>
  90. <entry><code>boolean</code></entry>
  91. <entry><code>false</code></entry>
  92. <entry>
  93. 如果设置为 true,核心将在 save() 方法里设置 ignore_user_abort PHP flag,以免在某些情况下缓存崩溃。
  94. </entry>
  95. </row>
  96. </tbody>
  97. </tgroup>
  98. </table>
  99. </sect3>
  100. <sect3 id="zend.cache.core.examples">
  101. <title>例子</title>
  102. <para>
  103. An example is given in the manual at the very beginning.
  104. </para>
  105. <para>
  106. 如果你只向缓存中存储字符串(由于"automatic_serialization"选项,可能会存储一些布尔值),你可以使用更加简介的构造:
  107. </para>
  108. <programlisting role="php"><![CDATA[
  109. // 假定你已经有 $cache
  110. $id = 'myBigLoop'; // cache id of "what we want to cache"
  111. if (!($data = $cache->load($id))) {
  112. // cache miss
  113. $data = '';
  114. for ($i = 0; $i < 10000; $i++) {
  115. $data = $data . $i;
  116. }
  117. $cache->save($data);
  118. }
  119. // [...] do something with $data (echo it, pass it on etc.)
  120. ]]>
  121. </programlisting>
  122. <para>
  123. 如果你缓存多个块或则数据实例,意思是一样的:
  124. </para>
  125. <programlisting role="php"><![CDATA[
  126. // 确保使用独一无二的 identifiers:
  127. $id1 = 'foo';
  128. $id2 = 'bar';
  129. // block 1
  130. if (!($data = $cache->load($id1))) {
  131. // cache missed
  132. $data = '';
  133. for ($i=0;$i<10000;$i++) {
  134. $data = $data . $i;
  135. }
  136. $cache->save($data);
  137. }
  138. echo($data);
  139. // this isn't affected by caching
  140. echo('NEVER CACHED! ');
  141. // block 2
  142. if (!($data = $cache->load($id2))) {
  143. // cache missed
  144. $data = '';
  145. for ($i=0;$i<10000;$i++) {
  146. $data = $data . '!';
  147. }
  148. $cache->save($data);
  149. }
  150. echo($data);
  151. ]]>
  152. </programlisting>
  153. <para>
  154. 如果你想缓存特殊值(带 "automatic_serialization" 选项的布尔值)或不能用上述紧缩结构的空字符串,你需要正式地测试缓存记录。
  155. </para>
  156. <programlisting role="php"><![CDATA[
  157. // the compact construction
  158. // (not good if you cache empty strings and/or booleans)
  159. if (!($data = $cache->load($id))) {
  160. // cache missed
  161. // [...] we make $data
  162. $cache->save($data);
  163. }
  164. // we do something with $data
  165. // [...]
  166. // the complete construction (works in any case)
  167. if (!($cache->test($id))) {
  168. // cache missed
  169. // [...] we make $data
  170. $cache->save($data);
  171. } else {
  172. // cache hit
  173. $data = $cache->load($id);
  174. }
  175. // we do something with $data
  176. ]]>
  177. </programlisting>
  178. </sect3>
  179. </sect2>
  180. <sect2 id="zend.cache.frontends.output">
  181. <title>Zend_Cache_Frontend_Output</title>
  182. <sect3 id="zend.cache.frontends.output.introduction">
  183. <title>简介</title>
  184. <para>
  185. <code>Zend_Cache_Frontend_Output</code> 是一个输出捕捉前端.它在PHP中使用输出缓冲捕获<code>start()</code> 和
  186. <code>end()</code> 方法间的一切输出.
  187. </para>
  188. </sect3>
  189. <sect3 id="zend.cache.frontends.output.options">
  190. <title>可用的选项</title>
  191. <para>
  192. 该前端除了<code>Zend_Cache_Core</code>那些选项外没有任何特定的选项.
  193. </para>
  194. </sect3>
  195. <sect3 id="zend.cache.frontends.output.examples">
  196. <title>例子</title>
  197. <para>
  198. An example is given in the manual at the very beginning. Here it is with minor changes:
  199. </para>
  200. <programlisting role="php"><![CDATA[
  201. // if it is a cache miss, output buffering is triggered
  202. if (!($cache->start('mypage'))) {
  203. // output everything as usual
  204. echo 'Hello world! ';
  205. echo 'This is cached ('.time().') ';
  206. $cache->end(); // output buffering ends
  207. }
  208. echo 'This is never cached ('.time().').';
  209. ]]>
  210. </programlisting>
  211. <para>
  212. Using this form it is fairly easy to set up output caching in your already working project
  213. with little or no code refactoring.
  214. </para>
  215. </sect3>
  216. </sect2>
  217. <sect2 id="zend.cache.frontends.function">
  218. <title>Zend_Cache_Frontend_Function</title>
  219. <sect3 id="zend.cache.frontends.function.introduction">
  220. <title>Introduction</title>
  221. <para>
  222. <code>Zend_Cache_Frontend_Function</code> caches the results of function calls. It has
  223. a single main method named <code>call()</code> which takes a function name and parameters
  224. for the call in an array.
  225. </para>
  226. </sect3>
  227. <sect3 id="zend.cache.frontends.function.options">
  228. <title>A可用的选项</title>
  229. <table id="zend.cache.frontends.function.options.table">
  230. <title>函数前端选项</title>
  231. <tgroup cols="4">
  232. <thead>
  233. <row>
  234. <entry>选项</entry>
  235. <entry>数据类型</entry>
  236. <entry>默认值</entry>
  237. <entry>描述</entry>
  238. </row>
  239. </thead>
  240. <tbody>
  241. <row>
  242. <entry><code>cache_by_default</code></entry>
  243. <entry><code>boolean</code></entry>
  244. <entry><code>true</code></entry>
  245. <entry>
  246. 如果为true,默认情况下,函数调用将被缓存.
  247. </entry>
  248. </row>
  249. <row>
  250. <entry><code>cached_functions</code></entry>
  251. <entry><code>array</code></entry>
  252. <entry></entry>
  253. <entry>
  254. 函数名称总是被缓存
  255. </entry>
  256. </row>
  257. <row>
  258. <entry><code>non_cached_functions</code></entry>
  259. <entry><code>array</code></entry>
  260. <entry></entry>
  261. <entry>
  262. 决不缓存函数名称
  263. </entry>
  264. </row>
  265. </tbody>
  266. </tgroup>
  267. </table>
  268. </sect3>
  269. <sect3 id="zend.cache.frontends.function.examples">
  270. <title>例子</title>
  271. <para>
  272. 在PHP中使用 <code>call()</code> 函数于使用 <code>call_user_func_array()</code>相同:
  273. </para>
  274. <programlisting role="php"><![CDATA[
  275. $cache->call('veryExpensiveFunc', $params);
  276. // $params is an array
  277. // For example to call veryExpensiveFunc(1, 'foo', 'bar') with
  278. // caching, you can use
  279. // $cache->call('veryExpensiveFunc', array(1, 'foo', 'bar'))
  280. ]]>
  281. </programlisting>
  282. <para>
  283. <code>Zend_Cache_Frontend_Function</code> is smart enough to cache both the
  284. return value of the function and its internal output.
  285. </para>
  286. <note><para>
  287. You can pass any built in or user defined function with the exception of
  288. <code>array()</code>, <code>echo()</code>, <code>empty()</code>, <code>eval()</code>,
  289. <code>exit()</code>, <code>isset()</code>, <code>list()</code>, <code>print()</code>
  290. and <code>unset()</code>.
  291. </para></note>
  292. </sect3>
  293. </sect2>
  294. <sect2 id="zend.cache.frontends.class">
  295. <title>Zend_Cache_Frontend_Class</title>
  296. <sect3 id="zend.cache.frontends.class.introduction">
  297. <title>Introduction</title>
  298. <para>
  299. <code>Zend_Cache_Frontend_Class</code> is different from <code>Zend_Cache_Frontend_Function</code>
  300. because it allows caching of object and static method calls.
  301. </para>
  302. </sect3>
  303. <sect3 id="zend.cache.frontends.class.options">
  304. <title>Available options</title>
  305. <table id="zend.cache.frontends.class.options.table">
  306. <title>Class frontend options</title>
  307. <tgroup cols="4">
  308. <thead>
  309. <row>
  310. <entry>Option</entry>
  311. <entry>Data Type</entry>
  312. <entry>Default Value</entry>
  313. <entry>Description</entry>
  314. </row>
  315. </thead>
  316. <tbody>
  317. <row>
  318. <entry><code>cached_entity</code> (required)</entry>
  319. <entry><code>mixed</code></entry>
  320. <entry></entry>
  321. <entry>
  322. if set to a class name, we will cache an abstract class and will use
  323. only static calls; if set to an object, we will cache this object methods
  324. </entry>
  325. </row>
  326. <row>
  327. <entry><code>cache_by_default</code></entry>
  328. <entry><code>boolean</code></entry>
  329. <entry><code>true</code></entry>
  330. <entry>
  331. if true, calls will be cached by default
  332. </entry>
  333. </row>
  334. <row>
  335. <entry><code>cached_methods</code></entry>
  336. <entry><code>array</code></entry>
  337. <entry></entry>
  338. <entry>
  339. method names which will always be cached
  340. </entry>
  341. </row>
  342. <row>
  343. <entry><code>non_cached_methods</code></entry>
  344. <entry><code>array</code></entry>
  345. <entry></entry>
  346. <entry>
  347. method names which must never be cached
  348. </entry>
  349. </row>
  350. </tbody>
  351. </tgroup>
  352. </table>
  353. </sect3>
  354. <sect3 id="zend.cache.frontends.class.examples">
  355. <title>Examples</title>
  356. <para>
  357. For example, to cache static calls :
  358. </para>
  359. <programlisting role="php"><![CDATA[
  360. class test {
  361. // Static method
  362. public static function foobar($param1, $param2) {
  363. echo "foobar_output($param1, $param2)";
  364. return "foobar_return($param1, $param2)";
  365. }
  366. }
  367. // [...]
  368. $frontendOptions = array(
  369. 'cached_entity' => 'Test' // The name of the class
  370. );
  371. // [...]
  372. // The cached call
  373. $result = $cache->foobar('1', '2');
  374. ]]>
  375. </programlisting>
  376. <para>
  377. To cache classic method calls :
  378. </para>
  379. <programlisting role="php"><![CDATA[
  380. class Test {
  381. private $_string = 'hello !';
  382. public function foobar2($param1, $param2) {
  383. echo($this->_string);
  384. echo "foobar2_output($param1, $param2)";
  385. return "foobar2_return($param1, $param2)";
  386. }
  387. }
  388. // [...]
  389. $frontendOptions = array(
  390. 'cached_entity' => new Test() // An instance of the class
  391. );
  392. // [...]
  393. // The cached call
  394. $result = $cache->foobar2('1', '2');
  395. ]]>
  396. </programlisting>
  397. </sect3>
  398. </sect2>
  399. <sect2 id="zend.cache.frontends.file">
  400. <title>Zend_Cache_Frontend_File</title>
  401. <sect3 id="zend.cache.frontends.file.introduction">
  402. <title>Introduction</title>
  403. <para>
  404. <code>Zend_Cache_Frontend_File</code> is a frontend driven by the
  405. modification time of a "master file". It's really interesting for examples
  406. in configuration or templates issues.
  407. </para>
  408. <para>
  409. For instance, you have an XML configuration file which is parsed by a function
  410. which returns a "config object" (like with <code>Zend_Config</code>). With
  411. <code>Zend_Cache_Frontend_File</code>, you can store the "config object" into
  412. cache (to avoid the parsing of the XML config file at each time) but with a
  413. sort of strong dependency on the "master file". So, if the XML config file
  414. is modified, the cache is immediately invalidated.
  415. </para>
  416. </sect3>
  417. <sect3 id="zend.cache.frontends.file.options">
  418. <title>Available options</title>
  419. <table id="zend.cache.frontends.file.options.table">
  420. <title>File frontend options</title>
  421. <tgroup cols="4">
  422. <thead>
  423. <row>
  424. <entry>Option</entry>
  425. <entry>Data Type</entry>
  426. <entry>Default Value</entry>
  427. <entry>Description</entry>
  428. </row>
  429. </thead>
  430. <tbody>
  431. <row>
  432. <entry><code>master_file (mandatory)</code></entry>
  433. <entry><code>string</code></entry>
  434. <entry><code></code></entry>
  435. <entry>
  436. the complete path and name of the master file
  437. </entry>
  438. </row>
  439. </tbody>
  440. </tgroup>
  441. </table>
  442. </sect3>
  443. <sect3 id="zend.cache.frontends.file.examples">
  444. <title>Examples</title>
  445. <para>
  446. Use of this frontend is the same than of <code>Zend_Cache_Core</code>. There
  447. is no need of a specific example - the only thing to do is to
  448. define the <code>master_file</code> when using the factory.
  449. </para>
  450. </sect3>
  451. </sect2>
  452. <sect2 id="zend.cache.frontends.page">
  453. <title>Zend_Cache_Frontend_Page</title>
  454. <sect3 id="zend.cache.frontends.page.introduction">
  455. <title>Introduction</title>
  456. <para>
  457. <code>Zend_Cache_Frontend_Page</code> is like <code>Zend_Cache_Frontend_Output</code>
  458. but designed for a complete page. It's impossible to use <code>Zend_Cache_Frontend_Page</code>
  459. for caching only a single block.
  460. </para>
  461. <para>
  462. On the other hand, the "cache id" is calculated automatically with
  463. <code>$_SERVER['REQUEST_URI']</code> and (depending on options)
  464. <code>$_GET</code>, <code>$_POST</code>, <code>$_SESSION</code>, <code>$_COOKIE</code>, <code>$_FILES</code>.
  465. More over, you have only one method to call (<code>start()</code>) because the
  466. <code>end()</code> call is fully automatic when the page is ended.
  467. </para>
  468. <para>
  469. For the moment, it's not implemented but we plan to add a HTTP conditional system
  470. to save bandwidth (the system will send a HTTP 304 Not Modified if the cache is hit
  471. and if the browser has already the good version).
  472. </para>
  473. </sect3>
  474. <sect3 id="zend.cache.frontends.page.options">
  475. <title>Available options (for this frontend in Zend_Cache factory)</title>
  476. <table id="zend.cache.frontends.page.options.table">
  477. <title>Page frontend options</title>
  478. <tgroup cols="4">
  479. <thead>
  480. <row>
  481. <entry>Option</entry>
  482. <entry>Data Type</entry>
  483. <entry>Default Value</entry>
  484. <entry>Description</entry>
  485. </row>
  486. </thead>
  487. <tbody>
  488. <row>
  489. <entry><code>http_conditional</code></entry>
  490. <entry><code>boolean</code></entry>
  491. <entry><code>false</code></entry>
  492. <entry>
  493. use the http_conditional system (not implemented for the moment)
  494. </entry>
  495. </row>
  496. <row>
  497. <entry><code>debug_header</code></entry>
  498. <entry><code>boolean</code></entry>
  499. <entry><code>false</code></entry>
  500. <entry>
  501. if true, a debug text is added before each cached pages
  502. </entry>
  503. </row>
  504. <row>
  505. <entry><code>default_options</code></entry>
  506. <entry><code>array</code></entry>
  507. <entry><code>array(...see below...)</code></entry>
  508. <entry>
  509. an associative array of default options :
  510. <itemizedlist>
  511. <listitem>
  512. <para>
  513. <code>(boolean, true by default) cache</code> :
  514. cache is on if true
  515. </para>
  516. </listitem>
  517. <listitem>
  518. <para>
  519. <code>(boolean, false by default) cache_with_get_variables</code> :
  520. if true, cache is still on even if there are some variables in
  521. <code>$_GET</code> array
  522. </para>
  523. </listitem>
  524. <listitem>
  525. <para>
  526. <code>(boolean, false by default) cache_with_post_variables</code> :
  527. if true, cache is still on even if there are some variables in
  528. <code>$_POST</code> array
  529. </para>
  530. </listitem>
  531. <listitem>
  532. <para>
  533. <code>(boolean, false by default) cache_with_session_variables</code> :
  534. if true, cache is still on even if there are some variables in
  535. <code>$_SESSION</code> array
  536. </para>
  537. </listitem>
  538. <listitem>
  539. <para>
  540. <code>(boolean, false by default) cache_with_files_variables</code> :
  541. if true, cache is still on even if there are some variables in
  542. <code>$_FILES</code> array
  543. </para>
  544. </listitem>
  545. <listitem>
  546. <para>
  547. <code>(boolean, false by default) cache_with_cookie_variables</code> :
  548. if true, cache is still on even if there are some variables in
  549. <code>$_COOKIE</code> array
  550. </para>
  551. </listitem>
  552. <listitem>
  553. <para>
  554. <code>(boolean, true by default) make_id_with_get_variables</code> :
  555. if true, the cache id will be dependent of the content of the
  556. <code>$_GET</code> array
  557. </para>
  558. </listitem>
  559. <listitem>
  560. <para>
  561. <code>(boolean, true by default) make_id_with_post_variables</code> :
  562. if true, the cache id will be dependent of the content of the
  563. <code>$_POST</code> array
  564. </para>
  565. </listitem>
  566. <listitem>
  567. <para>
  568. <code>(boolean, true by default) make_id_with_session_variables</code> :
  569. if true, the cache id will be dependent of the content of the
  570. <code>$_SESSION</code> array
  571. </para>
  572. </listitem>
  573. <listitem>
  574. <para>
  575. <code>(boolean, true by default) make_id_with_files_variables</code> :
  576. if true, the cache id will be dependent of the content of the
  577. <code>$_FILES</code> array
  578. </para>
  579. </listitem>
  580. <listitem>
  581. <para>
  582. <code>(boolean, true by default) make_id_with_cookie_variables</code> :
  583. if true, the cache id will be dependent of the content of the
  584. <code>$_COOKIE</code> array
  585. </para>
  586. </listitem>
  587. </itemizedlist>
  588. </entry>
  589. </row>
  590. <row>
  591. <entry><code>regexps</code></entry>
  592. <entry><code>array</code></entry>
  593. <entry><code>array()</code></entry>
  594. <entry>
  595. an associative array to set options only for some REQUEST_URI, keys are (PCRE) regexps, values are
  596. associative arrays with specific options to set if the regexp matchs on $_SERVER['REQUEST_URI']
  597. (see default_options for the list of available options) ; if several regexps match the $_SERVER['REQUEST_URI'],
  598. only the last one will be used
  599. </entry>
  600. </row>
  601. <row>
  602. <entry><code>memorize_headers</code></entry>
  603. <entry><code>array</code></entry>
  604. <entry><code>array()</code></entry>
  605. <entry>
  606. 对应于一些 HTTP 头名称的字符串数组。列表中的头将保存在缓存里,需要的时候就调出来。
  607. </entry>
  608. </row>
  609. </tbody>
  610. </tgroup>
  611. </table>
  612. </sect3>
  613. <sect3 id="zend.cache.frontends.page.examples">
  614. <title>Examples</title>
  615. <para>
  616. Use of Zend_Cache_Frontend_Page is really trivial :
  617. </para>
  618. <programlisting role="php"><![CDATA[
  619. // [...] // require, configuration and factory
  620. $cache->start();
  621. // if the cache is hit, the result is sent to the browser and the script stop here
  622. // rest of the page ...
  623. ]]>
  624. </programlisting>
  625. <para>
  626. a more complex example which shows a way to get a centralized cache management in a bootstrap file
  627. (for using with Zend_Controller for example)
  628. </para>
  629. <programlisting role="php"><![CDATA[
  630. /*
  631. * you should avoid putting too many lines before the cache section.
  632. * For example, for optimal performances, "require_once" or
  633. * "Zend_Loader::loadClass" should be after the cache section.
  634. */
  635. $frontendOptions = array(
  636. 'lifetime' => 7200,
  637. 'debug_header' => true, // for debugging
  638. 'regexps' => array(
  639. // cache the whole IndexController
  640. '^/$' => array('cache' => true),
  641. // cache the whole IndexController
  642. '^/index/' => array('cache' => true),
  643. // we don't cache the ArticleController...
  644. '^/article/' => array('cache' => false),
  645. // ... but we cache the "view" action of this ArticleController
  646. '^/article/view/' => array(
  647. 'cache' => true,
  648. // and we cache even there are some variables in $_POST
  649. 'cache_with_post_variables' => true,
  650. // but the cache will be dependent on the $_POST array
  651. 'make_id_with_post_variables' => true
  652. )
  653. )
  654. );
  655. $backendOptions = array(
  656. 'cache_dir' => '/tmp/'
  657. );
  658. // getting a Zend_Cache_Frontend_Page object
  659. $cache = Zend_Cache::factory('Page',
  660. 'File',
  661. $frontendOptions,
  662. $backendOptions);
  663. $cache->start();
  664. // if the cache is hit, the result is sent to the browser and the
  665. // script stop here
  666. // [...] the end of the bootstrap file
  667. // these lines won't be executed if the cache is hit
  668. ]]>
  669. </programlisting>
  670. </sect3>
  671. <sect3 id="zend.cache.frontends.page.cancel">
  672. <title> 特殊的取消方法 </title>
  673. <para>
  674. 因为设计问题,在有些情况下(例如使用非 HTTP/200 返回代码时),你可能需要取消当前缓存处理,所以
  675. 我们引入这个特别的前端,cancel()方法。
  676. </para>
  677. <programlisting role="php"><![CDATA[
  678. // [...] // require, configuration and factory
  679. $cache->start();
  680. // [...]
  681. if ($someTest) {
  682. $cache->cancel();
  683. // [...]
  684. }
  685. // [...]
  686. ]]>
  687. </programlisting>
  688. </sect3>
  689. </sect2>
  690. </sect1>
  691. <!--
  692. vim:se ts=4 sw=4 et:
  693. -->