本文實例講述了YII框架頁面緩存操作。分享給大家供大家參考,具體如下:
IndexController.php
namespace frontend\controllers; use yii; use yii\web\Controller; class IndexController extends Controller { public function behaviors()//先于action執(zhí)行,可以用來實現(xiàn)頁面緩存 { return [ [ 'class'=>'yii\filters\PageCache',//整個頁面緩存 'duration'=>10,//緩存時間 'only'=>['cache'],//只有index操作會被緩存,即使沒有視圖展示也會緩存 'dependency'=>[ 'class'=>'yii\caching\DbDependency', 'sql'=>'select count(*) from user', ], ] ]; } public function actionCache(){ //片段緩存 return $this->renderPartial("index"); } }
views/index/index.php
?php /** * Created by PhpStorm. * Date: 2016/5/25 * Time: 19:37 */ $duration = 15; //緩存依賴 $dependency = [ 'class'=>'yii\caching\FileDependency', 'fileName'=>'hw.txt',//web目錄下 ]; //緩存的開關 $enabled = false; ?> ?php //if($this->beginCache('cache_div',['duration' => $duration])){ //if($this->beginCache('cache_div',['enabled' => $enabled])){ if($this->beginCache('cache_div',['dependency' => $dependency])){?> div id="cache_div"> div>這里待會會被緩存 哈哈/div> /div> ?php $this->endCache(); }?> div id="no_cache_div"> div>這里不會被緩存 嚕/div> /div>
更多關于Yii相關內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。
上一篇:YII框架http緩存操作示例