Example of YII Framework Page Caching Operation
- 2021-12-11 06:59:28
- OfStack
This article illustrates the YII framework page caching operation. Share it for your reference, as follows:
IndexController.php
namespace frontend\controllers;
use yii;
use yii\web\Controller;
class IndexController extends Controller
{
public function behaviors()// Prior to action Execute , Can be used to implement page caching
{
return [
[
'class'=>'yii\filters\PageCache',// Full page cache
'duration'=>10,// Cache time
'only'=>['cache'],// Only index Actions are cached, even if there is no view display
'dependency'=>[
'class'=>'yii\caching\DbDependency',
'sql'=>'select count(*) from user',
],
]
];
}
public function actionCache(){
// Fragment cache
return $this->renderPartial("index");
}
}
views/index/index.php
<?php
/**
* Created by PhpStorm.
* Date: 2016/5/25
* Time: 19:37
*/
$duration = 15;
// Cache dependency
$dependency = [
'class'=>'yii\caching\FileDependency',
'fileName'=>'hw.txt',//web Directory
];
// Switch of cache
$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> This will be cached later Ha ha </div>
</div>
<?php
$this->endCache();
}?>
<div id="no_cache_div">
<div> It will not be cached here Lu </div>
</div>
More readers interested in Yii can check the topics of this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Basic Tutorial of Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Summary of Usage of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"
Hope that this article is based on Yii framework of PHP programming help.