Yii 1.0 Cache Code Demo
{Normal Cache}
//data cache public function actionTestCache() { // print_r(Yii::app()->cache); //check the value $cache = Yii::app()->cache; $time = $cache->get('BoyLee001'); //get cache if (false === $time){ //if cache is gone. $cache->set('BoyLee001',time(),30); //set cache // $cache['BoyLee001'] = $time(); //this is not right. $time = $cache->get('BoyLee001'); } echo $time; }
{Fregment Cache}
//fregment cache public function actionFregmentCache() { if ($this->beginCache('BoyLee002', 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 'BoyLee'.time(); echo @$_GET['id']; echo ' <hr /> '; $this->endCache(); } }
{PageCache}
{controller}
public function filters() { return array( array('COutputCache + PageCache', 'duration'=>30,'varyByParam'=>array('id')), ); } public function actionPageCache() { $this->render('pageCache'); }
{view pageCache.php}
echo time(); echo ' <hr /> '; 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.
Leave Comment
Choi
2015-03-16 20:12:00Posts like this bregithn up my day. Thanks for taking the time.