使用zend_cache中的Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions)的几种方法
,此种方法使用于与用户无关的公共页面的页面静态化
一、写在index.php中
1、在index.php中加入以下代码
require_once 'Zend/Registry.php';
require_once 'Zend/Cache.php';
if (false == Zend_Registry::isRegistered('pagefileCache')) {
$frontendOptions = array(
'lifetime' => 24*3600, // 缓存寿命
'debug_header' => true, // true是打开debug,通常设为false
'regexps' => array(
'/' => array('cache' => true), // 所有的controller都缓存
'/index/' => array('cache'=>false), //首页不缓存(已做了用户持基信息缓存,和
局部页面缓存)
'/wdjk/' => array('cache'=>false), //我的金库不缓存(同上)
'/blnc/' => array('cache'=>false), //博览内参不缓存(有权限,等待测试)
'/zxzx/' => array('cache'=>false), //资讯中心不缓存(有权限,等待测试)
'/funddatabase/' => array('cache'=>true), //基金数据库缓存
'/knowledge/' => array('cache'=>true), //知识库缓存
),
'default_options' => array(
'cache_with_get_variables' => true, //cache_with_*默认值为false,
make_id_with_*默认值为true
'cache_with_post_variables' => true,
'cache_with_files_variables' => true,
'cache_with_session_variables' => true, // 注意如果开了session要把这个打开
'cache_with_cookie_variables' => true, // 注意如果开了session要把这个打开
'make_id_with_cookie_variables' => false, // 注意如果开了session要把这个打开
'make_id_with_session_variables' => false, // 注意如果开了session要把这个打开
)
);
$backendOptions = array(
'cache_dir' => 'D:/page', // 缓存存放路径,必须存在并可写
);
$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions);
Zend_Registry::set('pagefileCache', $cache);
}
2、在想要缓存的controller里面的init方法里面加入
$cache=Zend_Registry::get('pagefileCache');
$cache->start();
二、写在Bootstrap.php中
1、在Bootstrap.php中加入一个缓存方法
public function getPageFileCache()
{
if (false == Zend_Registry::isRegistered('pagefileCache')) {
$frontendOptions = array(
'lifetime' => 24*3600, // 缓存寿命
'debug_header' => true, // true是打开debug,通常设为false
'regexps' => array(
'/' => array('cache' => true), // 所有的controller都
缓存
'/index/' => array('cache'=>false), //此页面不缓存
'/knowledge/' => array('cache'=>true), //此页面缓存
),
'default_options' => array(
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_files_variables' => true,
'cache_with_session_variables' => true,
'cache_with_cookie_variables' => true,
'make_id_with_cookie_variables' => false,
'make_id_with_session_variables' => false,
)
);
$backendOptions = array(
'cache_dir' => 'D:/page',
);
$cache = Zend_Cache::factory('Page', 'File', $frontendOptions,
$backendOptions);
Zend_Registry::set('pagefileCache', $cache);
return $cache;
} else {
return Zend_Registry::get('pagefileCache');
}
}
2、在index.php页面里面new一个$bootstrap对象备用
if(false == Zend_Registry::isRegistered('bootstrap')) {
$bootstrap = new Bootstrap($application);
Zend_Registry::set('bootstrap', $bootstrap);
}
3、在想要缓存的controller里面的init方法里面加入
$bootstrap = Zend_Registry::get('bootstrap');
$cache = $bootstrap->getPageFileCache();
$cache->start();
三、封装为一个类里面的一个方法,在controller里面的init方法调用即可
注意:之前发现一个问题就是'regexps'中的设置没有效果,后来发现是正则匹配错了。网上的例子都是'^/index/' => array('cache'=>false),而正则中“^”表示以某字符串开头。使用的时候要注意。