您的位置:首页 > 编程语言 > PHP开发

Yii: 片段缓存的使用

2014-03-19 22:57 477 查看
Yii没有插件支持页面静态化,但很好的支持页面片段缓存,
虽然仍然需要经过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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: