Normal data caching
- // data cache
- public function actionCache()
- {
- $cache = Yii::app()->cache;
- //get cache
- $time = $cache->get('cache_key');
- //if cache is gone.
- if (false === $time){
- //set cache
- $cache->set('cache_key',time(),30);
- $time = $cache->get('cache_key');
- }
- echo $time;
- }
Fregment data caching
- // fregment data cache
- public function actionCache()
- {
- if ($this->beginCache('cache_key',
- array('duration'=>60,
- 'dependency'=>array(
- 'class'=>'system.caching.dependencies.CDbCacheDependency',//you must have db set here
- 'sql'=>'SELECT COUNT(*) FROM `user`',//you must have db set here
- ),
- 'varyByParam'=>array('id'),//cache by $_GET['id']
- ))){
- echo 'cache_key'.time();
- echo @$_GET['id'];
- echo ' <hr /> ';
- $this->endCache();
- }
- }
Page Cache
- public function filters()
- {
- return array(
- array('COutputCache + PageCache', 'duration'=>30,'varyByParam'=>array('id')),
- );
- }
- public function actionCache()
- {
- $this->render('pageCache');
- }
It's easy to use, just to be a bit change. More about yii caching visit this: http://www.yiiframework.com/doc/guide/1.1/en/caching.overview
No comments:
Post a Comment