7.2.Zeng

    xiaoxiao2026-08-19  1

    4.3. Zend_Cache前端

    4.3.1. Zend_Cache_Core

    4.3.1.1. 简介

    Zend_Cache_Core是一个特别的前端,因为他是模块的核心. 它是一个一般化(generic)的缓存前端,并且由其他类扩展.

    4.3.1.2. 可用选项 这些选项被传递给如前面例子中演示的工厂方法.


    4.3.1.3. 例子 An example is given in the manual at the very beginning.

    如果你只向缓存中存储字符串(由于”automatic_serialization”选项,可能会存储一些布尔值),你可以使用更加简介的构造:

    <?php // we assume you already have $cache $id = 'myBigLoop'; // cache id of "what we want to cache" if (!($data = $cache->load($id))) { // cache miss $data = ''; for ($i = 0; $i < 10000; $i++) { $data = $data . $i; } $cache->save($data); } // [...] do something with $data (echo it, pass it on etc.)

    如果你缓存多个块或则数据实例,意思是一样的:

    <?php // make sure you use unique identifiers: $id1 = 'foo'; $id2 = 'bar'; // block 1 if (!($data = $cache->load($id1))) { // cache missed $data = ''; for ($i=0;$i<10000;$i++) { $data = $data . $i; } $cache->save($data); } echo($data); // this isn't affected by caching echo('NEVER CACHED! '); // block 2 if (!($data = $cache->load($id2))) { // cache missed $data = ''; for ($i=0;$i<10000;$i++) { $data = $data . '!'; } $cache->save($data); } echo($data);

    如果你想缓存特殊值(带 “automatic_serialization” 选项的布尔值)或不能用上述紧缩结构的空字符串,你需要正式地测试缓存记录。

    <?php // the compact construction (not good if you cache empty strings and/or booleans) if (!($data = $cache->load($id))) { // cache missed // [...] we make $data $cache->save($data); } // we do something with $data // [...] // the complete construction (works in any case) if (!($cache->test($id))) { // cache missed // [...] we make $data $cache->save($data); } else { // cache hit $data = $cache->load($id); } // we do something with $data

    4.3.2. Zend_Cache_Frontend_Output

    4.3.2.1. 简介 Zend_Cache_Frontend_Output 是一个输出捕捉前端.它在PHP中使用输出缓冲捕获start() 和 end() 方法间的一切输出.

    4.3.2.2. 可用的选项 该前端除了Zend_Cache_Core那些选项外没有任何特定的选项.

    4.3.2.3. 例子 An example is given in the manual at the very beginning. Here it is with minor changes:

    <?php // if it is a cache miss, output buffering is triggered if (!($cache->start('mypage'))) { // output everything as usual echo 'Hello world! '; echo 'This is cached ('.time().') '; $cache->end(); // output buffering ends } echo 'This is never cached ('.time().').';

    Using this form it is fairly easy to set up output caching in your already working project with little or no code refactoring.


    4.3.3. Zend_Cache_Frontend_Function

    4.3.3.1. Introduction Zend_Cache_Frontend_Function caches the results of function calls. It has a single main method named call() which takes a function name and parameters for the call in an array.

    4.3.3.2. A可用的选项 表 4.2. 函数前端选项

    4.3.3.3. 例子 在PHP中使用 call() 函数于使用 call_user_func_array()相同:

    <?php $cache->call('veryExpensiveFunc', $params); # $params is an array # for example to call (with caching) veryExpensiveFunc(1, 'foo', 'bar'), you will use # $cache->call('veryExpensiveFunc', array(1, 'foo', 'bar'))

    转载请注明原文地址: https://ju.6miu.com/read-1311346.html
    最新回复(0)