首页 > 学院 > 开发设计 > 正文

34.缓存

2019-11-08 01:17:51
字体:
来源:转载
供稿:网友

这里写图片描述

[php]array( ...... 'components'=>array( ...... 'cache'=>array( 'class'=>'system.caching.CMemCache', 'servers'=>array( array('host'=>'server1', 'port'=>11211, 'weight'=>60), array('host'=>'server2', 'port'=>11211, 'weight'=>40), ), ), ),);

这里写图片描述 这里写图片描述


数据缓存

先在配置文件components数组中加上:'cache'=>array( 'class'=>'CFileCache'),

这里写图片描述

[php]$value=Yii::app()->cache->get($id);if($value===false){ // 因为在缓存中没找到 $value ,重新生成它 , // 并将它存入缓存以备以后使用: // Yii::app()->cache->set($id,$value);}

这里写图片描述


缓存依赖 这里写图片描述

[php]// 此值将在30秒后失效// 也可能因依赖的文件发生了变化而更快失效Yii::app()->cache->set($id, $value, 30, new CFileCacheDependency('FileName'));

这里写图片描述


片段缓存(Fragment Caching) 这里写图片描述

[php]...别的HTML内容...<?php if($this->beginCache($id)) { ?>...被缓存的内容...<?php $this->endCache(); } ?>...别的HTML内容...在上面的,如果[beginCache()|CBaseController::beginCache()] 返回false,缓存的内容将此地方自动插入; 否则,在if语句内的内容将被执行并在[endCache()|CBaseController::endCache()]触发时缓存。

这里写图片描述

[php]...其他HTML内容...<?php if($this->beginCache($id, array('duration'=>3600))) { ?>...被缓存的内容...<?php $this->endCache(); } ?>...其他HTML内容...

这里写图片描述

[php]...其他HTML内容...<?php if($this->beginCache($id, array('dependency'=>array( 'class'=>'system.caching.dependencies.CDbCacheDependency', 'sql'=>'SELECT MAX(lastModified) FROM Post')))) { ?>...被缓存的内容...<?php $this->endCache(); } ?>...其他HTML内容...

这里写图片描述

[php]...其他HTML内容...<?php if($this->beginCache($id, array('requestTypes'=>array('GET')))) { ?>...被缓存的内容...<?php $this->endCache(); } ?>...其他HTML内容...

这里写图片描述


页面缓存 这里写图片描述

[php]public function filters(){ return array( array( 'COutputCache', 'duration'=>100, 'varyByParam'=>array('id'), ), );}

这里写图片描述


动态内容(Dynamic Content) 这里写图片描述

[php]...别的HTML内容...<?php if($this->beginCache($id)) { ?>...被缓存的片段内容... <?php $this->renderDynamic($callback); ?>...被缓存的片段内容...<?php $this->endCache(); } ?>...别的HTML内容...

这里写图片描述


http://www.yiichina.com/doc/guide/1.1/caching.data


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表