yii框架的缓存

一、yii的缓冲需要在main.php里面配置,

/****************/

‘cache’ => array (

‘class’ => ‘system.caching.CFileCache’

)

配置完成以后只需在控制器里面….

Yii::app ()->cache->set($key,$value,$expire);

Yii::app ()->cache->get($key);

二、配置多种缓存

main.php 的配置

‘cache’ => array (

‘class’ => ‘system.caching.CFileCache’

),

‘dbcache’ => array (

‘class’ => ‘system.caching.CDbCache’

)

控制器只需用相应的cacheid调用就好

如:Yii::app ()->dbcache

三、页面缓冲也就是把整个页面静态化

main.php配置还是一样

控制器:可以使用COutputCache类作为一个我们控制器类中的一个行为过滤器。

public function filters() {

return array (

array (

‘COutputCache’,

‘duration’ => 600,

‘ varyByParam’ => array(‘id’)

)

);

}

COutputCache来缓存数据时,来作为自动生成key名字的策略。下面是可以使用的一个列表:

varyByRoute:通过将该选项设置为true,具体请求的路由部分将会作为独立标识符的一部分用于生成缓存数据。所以,你可以使用请求controller和acion的组合来区别缓存内容。

varyBySession:通过设置该选项为true,将使用唯一的session id来区分缓存中的内容。每个用户的session都是不同的,但是可以用来为缓存服务。

varyByParam:如前面所说,这里是用输入的GET中的参数来区分缓存内容。

varyByExpression:给该选项设置PHP表达式,我们可以使用相应表达式的结果来区分缓存的内容。

还有要注意的是 COutputCache默认的cacheid是cache

四、片段缓存

片段缓存用来缓存一个页面的一部分。我们可以在view脚本中使用片段缓存。我们通过使用CController::beginCache()和CController::endCache()方法来实现。

…some HTML content…

<?php if($this—>beginCache($key))?>

…content to be cached…

<?php $this—>endCache(); ?>

…other HTML content…

来源: http://www.birds-fish.com/?p=39

原文地址:https://www.cnblogs.com/zfying/p/2611845.html