Yii: 片段缓存的使用
2014-03-19 22:57
477 查看
Yii没有插件支持页面静态化,但很好的支持页面片段缓存,
虽然仍然需要经过PHP处理,考虑到网络因素,已经能和静态页面达到差不多的性能效果。
下面是代码示范,解决的问题是根据用户会话情况来判断缓存的使用:
如果使用的是文件缓存,该片段缓存保存在cachePath下面:
Yii还支持表达式缓存varyByExpression,比如上面的缓存判断可以改为:
'varyByExpression'=>Yii::app()->user->id。
以及支持数据依赖缓存,比如将上面的用例改写为:
参考链接:
http://www.yiiframework.com/doc/api/1.1/COutputCache#varyByParam-detail
by iefreer
虽然仍然需要经过PHP处理,考虑到网络因素,已经能和静态页面达到差不多的性能效果。
下面是代码示范,解决的问题是根据用户会话情况来判断缓存的使用:
<!-- header --> <?php if($this->beginCache('USER_DIV', array('varyBySession'=>1))) { ?> <?php Template::widget('CMS_HEADER', 'Site.Header'); ?> <?php $this->endCache(); } ?> <!-- header.end -->
如果使用的是文件缓存,该片段缓存保存在cachePath下面:
'cache'=>array( 'class'=>'system.caching.CFileCache', 'cachePath'=>'protected/runtime/yii_cached', ),
Yii还支持表达式缓存varyByExpression,比如上面的缓存判断可以改为:
'varyByExpression'=>Yii::app()->user->id。
以及支持数据依赖缓存,比如将上面的用例改写为:
<?php if($this->beginCache('USER_DIV', array( 'dependency'=>array( 'class'=>'system.caching.dependencies.CDbCacheDependency', 'sql'=>'SELECT MAX(LogTime) FROM users where id=1')))) { ?> ... content to be cached <?php $this->endCache(); } ?>
参考链接:
http://www.yiiframework.com/doc/api/1.1/COutputCache#varyByParam-detail
by iefreer
相关文章推荐
- Yii 2.0 使用片段缓存
- 使用JSP自定义标签做页面片段的缓存
- YII之片段缓存
- YII 片段缓存如何实现。
- Yii的缓存使用详细教程
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- 在YII中使用Redis等缓存
- Yii配置与使用memcached缓存的方法
- Yii之页面缓存的使用
- Yii的自带缓存的使用
- Yii框架官方指南系列30——缓存:片段缓存(Fragment Caching)
- YII之片段缓存
- Yii缓存方法使用(二)
- YII Framework学习教程-YII的缓存-使用
- 使用JSP自定义标签做页面片段的缓存
- yii CActiveDataProvider使用缓存
- yii缓存使用
- Yii 2.0如何使用页面缓存方法示例
- ASP.Net 页面缓存片段使用技巧
- Yii框架自带缓存的使用方法