本文實(shí)例講述了YII框架頁面緩存操作。分享給大家供大家參考,具體如下:
IndexController.php
namespace frontend\controllers; use yii; use yii\web\Controller; class IndexController extends Controller { public function behaviors()//先于action執(zhí)行,可以用來實(shí)現(xiàn)頁面緩存 { return [ [ 'class'=>'yii\filters\PageCache',//整個(gè)頁面緩存 'duration'=>10,//緩存時(shí)間 'only'=>['cache'],//只有index操作會(huì)被緩存,即使沒有視圖展示也會(huì)緩存 '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目錄下 ]; //緩存的開關(guān) $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>這里待會(huì)會(huì)被緩存 哈哈/div> /div> ?php $this->endCache(); }?> div id="no_cache_div"> div>這里不會(huì)被緩存 嚕/div> /div>
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
標(biāo)簽:麗江 鷹潭 重慶 巴彥淖爾 衡陽 臨沂 十堰 銅陵
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《YII框架頁面緩存操作示例》,本文關(guān)鍵詞 YII,框架,頁面,緩存,操作,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。