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 '
';
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 '
';
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.

你可能感兴趣的:(YII CACHE使用示例)