本文實例講述了YII框架頁面緩存操作。分享給大家供大家參考,具體如下:
IndexController.php
namespace frontend/controllers;use yii;use yii/web/Controller;class IndexController extends Controller{ public function behaviors()//先于action執行,可以用來實現頁面緩存 { 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框架的PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選