zend_cache用法总结(1)

使用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),而正则中“^”表示以某字符串开头。使用的时候要注意。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值