码迷,mamicode.com
首页 > 其他好文 > 详细

yii使用之缓存

时间:2017-03-17 11:00:14      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:turn   als   使用   this   嵌套   登录   ***   方法   pen   

1、片段缓存(针对于视图中的某部分进行缓存);

 1 <?php
 2 设置有效时间
 3 $time=15;
 4 缓存依赖,存入文件。当文件内容发生改变是才会刷新新内容
 5 $dependecy=[
 6 ‘class‘=>‘yii\caching\FileDependency‘,
 7 ‘fileName‘=>‘xxx.txt‘
 8 ];
 9 设置是否开启缓存
10 $enable=false/ture
11 
12 ?>
13 <?php if( $this->beginCache(‘缓存名‘,[‘duration‘=>$time,‘dependecy‘=>$dependecy,‘enable‘=>$enable]) ){ ?>
14 15 ****缓存的内容 16 内容将被缓存,再次访问时。直接读取缓存***** 17 18 <?php 19 $this->endCache(); 20 } 21 ?>

注意:当使用嵌套是就得注意内外内容的缓存时间了。有时候会出现内缓存已经过期了,但还是读的缓存文件,可能是你的外缓存还存在

 

2、页面缓存(在控制器中)

behaviors()该方法会在所有的方法执行前执行,当然你也可以用它来做一些手脚。比如做防非法操作,就可以在return前调一个判断是否登录的方法来控制你不登录就不能操作
 1 public function behaviors()
 2 {
 3 return [
 4 [
 5 ‘class‘=>‘yii\filters\PageCache‘,
 6 ‘duration‘=>1000,       //缓存的时间
 7 ‘only’=>[‘index‘,],    //对那个操作进行缓存
 8 ‘dependency‘=[
 9 ‘class‘=>‘yii\caching\FileDependency‘,
10 ‘fileName‘=>‘xxx.txt‘  //缓存的文件名
11 ];
12 
13 ]
14 ];
15 }

缓存就差不多是这些了,希望有帮助吧

 

yii使用之缓存

标签:turn   als   使用   this   嵌套   登录   ***   方法   pen   

原文地址:http://www.cnblogs.com/fwqblogs/p/6564432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!