Zend_Cache_Frontend_Page适合用在controller中对action及其页面进行精细的缓存,下面仅举一简单例子,希望大家举一反三:
// IndexController.phpclass IndexController extends Zend_Controller_Action { public function init() { $frontendOptions = array( 'lifetime' => 3600, // 缓存寿命 'debug_header' => true, // true是打开debug,通常设为false 'regexps' => array( '^/$' => array('cache' => true), // 所有页面都缓存 '^/index/' => array('cache' => true), // 缓存index下所有action页 '^/index/foo' => array('cache' => false), //对foo action不缓存 ), 'default_options' => array( 'cache_with_get_variables' => true, 'cache_with_post_variables' => true, 'make_id_with_cookie_variables' => true, // 注意如果开了session要把这个打开 'cache_with_session_variables' => true, // 注意如果开了session要把这个打开 'cache_with_files_variables' => true, 'cache_with_cookie_variables' => true, // 注意如果开了session要把这个打开 ) ); $backendOptions = array( 'cache_dir' => 'C:/repository/cache/', // 缓存存放路径,必须存在并可写 ); $cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions); $cache->start(); // 开始缓存 } public function indexAction() { // index.phtml 页面将被缓存 } public function fooAction() { // foo.phtml 页面将不被缓存 } } 复制代码 // bootstrap.phppublic function getPageCache() { if (false == Zend_Registry::isRegistered('pageCache')) { ... $cache = ... // 实例化你的cache Zend_Registry::set('pageCache', $cache); return $cache; } else { return Zend_Registry::get('pageCache'); } } 复制代码 在任何你需要的地方: $cache = $bootstrap->getPageCache();