YII框架页面缓存操作示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

YII框架页面缓存操作示例

  2021-04-03 我要评论

本文实例讲述了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程序设计有所帮助。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们