YII CACHE使用示例

从Boylee那抄过来的.链接中有boylee的博客地址. 我没有使用code插件,所以效果没有boy那的好,只是为了记录一下.如果需要复制代码之类的,请猛点此处

{Normal Cache}
01 //data cache
02 public function actionTestCache()
03 {
04 // print_r(Yii::app()->cache); //check the value
05 $cache = Yii::app()->cache;
06 $time = $cache->get('BoyLee001'); //get cache
07 if (false === $time){ //if cache is gone.
08 $cache->set('BoyLee001',time(),30); //set cache
09 // $cache['BoyLee001'] = $time(); //this is not right.
10 $time = $cache->get('BoyLee001');
11 }
12 echo $time;
13 }

{Fregment Cache}


01 //fregment cache
02 public function actionFregmentCache()
03 {
04 if ($this->beginCache('BoyLee002',
05 array('duration'=>60,
06 'dependency'=>array(
07 'class'=>'system.caching.dependencies.CDbCacheDependency',//you must have db set here
08 'sql'=>'SELECT COUNT(*) FROM `user`',//you must have db set here
09 ),
10 'varyByParam'=>array('id'),//cache by $_GET['id']
11 ))){
12 echo 'BoyLee'.time();
13 echo @$_GET['id'];
14 echo ' <hr /> ';
15 $this->endCache();
16 }
17 }

{PageCache}

{controller}
01 public function filters()
02 {
03 return array(
04 array('COutputCache + PageCache', 'duration'=>30,'varyByParam'=>array('id')),
05 );
06 }
07 public function actionPageCache()
08 {
09 $this->render('pageCache');
10 }

{view pageCache.php}
1 echo time();
2 echo ' <hr /> ';
3 echo @$_GET['id'];

{Boy Say}

It’s easy to use when you follow my code, just to be a bit change. I will update a post late for detail.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值