【详解】yii中缓存(cache)的使用

jerry Yii 2015年08月11日 收藏

一、缓存配置:

1、单一缓存组件配置:

Yii缓存可以在不同的级别使用。在最低级别,可用来缓存单个数据(数据缓存)。往上一级,我们缓存一个由视图脚本生成的页面片断(片段缓存)。在最高级别,可存储整个页面以便需要的时候直接从缓存读取。本文说明页面缓存的配置及实现效果;

实现分为2步;

1. 在config文件加入缓存组件.

  1. cache => array (
  2. class => system.caching.CFileCache’,
  3. directoryLevel => 2,
  4. ),


class标识需要使用的缓存媒介,用途比较广的类型基本都有支持:

CMemCache: 使用 PHP memcache 扩展.

CApcCache: 使用 PHP APC 扩展.

CDbCache: 使用一张数据库表来存储缓存数据。

CFileCache: 使用文件来存储缓存数据。 特别适用于大块数据(例如页面)。

当然,yii也可以支持Redis,需要装一个插件:

  1. http://www.yiibase.com/download/view/32.html


本文实例使用的是文件缓存,对于文件缓存,缓存到的位置为protected/runtime/;directoryLevel设置缓存文件的目录深度;如果缓存页面特别多,这个值需要设置大点,否则每个目录下的页面会很多;

对于除class其他的选项的配置,可以查看各class类中的一些属性

2. 在要做缓存的控制器里定义过滤器。

  1. public function filters() {
  2. return array (
  3. array (
  4. COutputCache + post, list’,
  5. duration => 3600,
  6. varyByParam => array(‘id’,'page’),
  7. ‘dependency’ => array(
  8. ‘class’=>’CDbCacheDependency’,
  9. ‘sql’=>’SELECT MAX(id) FROM me115_book’,
  10. )
  11. );
  12. }


COutputCache 是用于处理缓存的类,如果只填’COutputCache’,则控制器里所有action都会通过缓存过滤,定义’COutputCache + post, list’,表示只对以下方法进行缓存:actionPost, actionList

duration 是缓存的时间,单位是秒,

varyByParam 是指定一系列GET参数名称列表, 使用相应的值去确定缓存内容的版本,即同一个action用于区分是不同页面的的参数,此处我以id和page来区分不同页面。

除varyByParam以外,还可以采用其他的条件来区分页面:

varyByExpression:指定缓存内容通过自定义的PHP表达式的结果而变化

varyByRoute:指定缓存内容基于请求的路由不同而变化 (controller 和 action)

varyBySession:指定是否缓存内容. 因用户session不同而变化

dependency’指定缓存失效依赖关系:可指定文件或数据库;本文采用的是数据库依赖CDbCacheDependency;

本例指定的是数据库,通过数据表的某个值的变化来确定缓存是否失效。例如,如果在表中新增了一条me115_book记录,即使缓存才过了2分钟(<3600),仍然判断为失效,从而查询数据库,生成整个页面,再次缓存;

检查:

查看当前页面是否缓存,可以dump输出一个当前服务器时间,从而检查当前页面是否已缓存;

优化效果:

优化站点为一个博客站点(me115.com),除了DNS解析转接外,未进行任何优化,优化前的数据为:
v6Vrua[1].jpg

首字节时间为842ms;

采用页面缓存之后的效果:
jAJrmi[1].jpg

首字节时间为376ms;html生成的时间大大缩短,后台时间减少了一倍。

当然,通过本图可以看到整个站点的用时还是比较长,主要是在页面组件(css/js/图片)上的下载耗费了不少时间,后续将针对这方面进行前端优化;

2、多缓存组件配置:

//不管是多缓存配置还是单缓存配置,都需要设置一个默认的cache组件,因为CDbConnection中的 schemaCacheID ,queryCacheID,CCacheHttpSession 中的cacheID ,CUrlManager 中的cacheID, CStatePersister 中的cacheID都是默认的 ’cache'

  1. 'cache' => array(
  2. 'class' => 'CFileCache',
  3. ),
  4. 'ApcCache'=>array(
  5. 'class'=>'CApcCache',
  6. ),
  7. 'Dbcache'=>array(
  8. 'class'=>'CDbCache',
  9. ),
  10. 'FileCache'=>array(
  11. 'class'=>'CFileCache',
  12. 'cachePath'=> '/Webroot/trackstar/protected/runtime/cache/test',
  13. ),
  14. 'MemCache'=>array(
  15. 'class'=>'CMemCache',
  16. 'servers'=>array(
  17. array(
  18. 'host'=>'server1',
  19. 'port'=>11211,
  20. 'weight'=>60,
  21. ),
  22. array(
  23. 'host'=>'server2',
  24. 'port'=>11211,
  25. 'weight'=>40,
  26. ),
  27. ),
  28. ),


session cache在开启apc cache时可以用,它将seesion存到apc中比存到文件中要快
  

  1.   'cache' => array(  
  2.         'class' => 'CApcCache',  
  3.     ),  
  4.     'session' => array(  
  5.         'class' => 'CCacheHttpSession',   //CCacheHttpSession在system.web包里
  6.     ),


yii如果要使用session.save_handler=memcache 如果需要扩展自定义的session管理方法 ,仅仅需要继承CHttpSession, 重写openSession,readSession,writeSession, destroySession,gcSession 这五个方法即可
3、数据库缓存配置:
//main.php配置文件中 数据库缓存配置,具体参照CDbConnection

  1. 'schemaCachingDuration' => 3600, //缓存时间
  2. 'schemaCachingExclude' => array(), //不需要缓存的表名数组
  3. 'schemaCacheID' => 'cache', //使用的缓存组件


此外,CDbConnection中还有关于数据库查询的缓存配置,不过这里只是一个全局的配置
//查询缓存配置

  1. 'queryCachingDuration' => 3600,//缓存时间
  2. 'queryCachingDependency' => null, //缓存依赖
  3. 'queryCachingCount' => 2, //第一次使用这条sql语句后同样的多少条sql语句需要缓存
  4. 'queryCacheID' => 'cache', //使用的缓存组件

需要配置数据库查询缓存,可以使用使用以下方法:
一、CDbConnection中的cache方法
public CDbConnection cache(integer $duration, CCacheDependency $dependency=NULL,integer $queryCount=1)
$duration integer 查询结果保持在缓存中有效的秒数。如果它是0,缓存将被禁用。

$dependency CCacheDependency    当查询结果保存到缓存时,使用的依赖。
$queryCount integer 在调用此方法后,需要缓存的SQL查询的数目。默认为 1,意味着下一条SQL查询将被缓存。
{return}    CDbConnection   返回连接实例本身。

  1. public function cache($duration, $dependency=null, $queryCount=1)
  2. {
  3. $this->queryCachingDuration=$duration;
  4. $this->queryCachingDependency=$dependency;
  5. $this->queryCachingCount=$queryCount;
  6. return $this;
  7. }

例子:

  1. $sql = 'SELECT * FROM tbl_post LIMIT 20';  
  2. $dependency = new CDbCacheDependency('SELECT MAX(update_time) FROM tbl_post');  
  3. $rows = Yii::app()->db->cache(1000, $dependency)->createCommand($sql)->queryAll();


二、CActiveRecord中的cache方法:
public CActiveRecord cache(integer $duration, CCacheDependency $dependency=NULL, integer $queryCount=1)
$duration integer 查询结果可能保持在缓存中有效的秒数。如果这是0,缓存将被禁用。
$dependency CCacheDependency 保存查询结果到缓存时使用的依赖关系。
$queryCount integer 在调用此方法后,需要缓存的SQL查询的数目。 默认值为 1,这意味着下一个SQL查询将被缓存。
{return} CActiveRecord AR实例本身。

  1. public function cache($duration, $dependency=null, $queryCount=1)
  2. { 
  3. //实际上他调用的是CDbConnection中的cache方法
  4. $this->getDbConnection()->cache($duration, $dependency, $queryCount);
  5. return $this;
  6. }


例子:

  1. $dependency = new CDbCacheDependency('SELECT MAX(update_time) FROM tbl_post'); 
  2. $posts = Post::model()->cache(1000, $dependency)->findAll();


关于$queryCount:
启用查询缓存

要使用查询缓存,首先要确保CDbConnection::queryCacheID指向一个可用的缓存组件ID(默认是cache)。

用DAO查询缓存

要使用查询缓存,我们在数据库查询的时候调用CDbConnection::cache()这个方法。例如:

  1. $sql = 'SELECT * FROM tbl_post LIMIT 20';
  2. $dependency = new CDbCacheDependency('SELECT MAX(update_time) FROM tbl_post');
  3. $rows = Yii::app()->db->cache(1000, $dependency)->createCommand($sql)->queryAll();


执行以上语句的时候,Yii会首先检查一下缓存是否包含一个该语句的查询结果。检查步骤是以下的三个条件:

●如果缓存包含了SQL语句中的入口索引
●如果入口还没过期(少于保存后的1000秒)
●如果依赖关系没有变化(update_time的最大值是跟查询结果保存到缓存时一致)

如果以上3个条件都满足了,缓存的结果就会直接返回给请求。否则,SQL语句就会被传递到数据库系统去执行,得到的结果会保存到缓存,返回给请求。

用AR查询缓存

我们也可以用AR来查询缓存。我们用一个类似的方法,调用CActiveRecord::cache():

  1. $dependency = new CDbCacheDependency('SELECT MAX(update_time) FROM tbl post');
  2. $posts = Post::model()->cache(1000, $dependency)->findAll();
  3. // relational AR query
  4. $posts = Post::model()->cache(1000, $dependency)->with('author')->findAll();


上面的cache()方法,实际上是CDbConnection::cache()的快捷方式。在内部,当执行AR的查询语句是,Yii会尝试我们之前讲述过的查询缓存。

缓存的多种查询

默认情况下,我们每次调用cache()(不管是CDbConnection 还是 CActiveRecord),都会标记下次要缓存的SQL,其他任何的SQL查询都不会被缓存,除非我们再次调用cache(),例如:

  1. $sql = 'SELECT * FROM tbl post LIMIT 20';
  2. $dependency = new CDbCacheDependency('SELECT MAX(update_time) FROM tbl post');
  3. $rows = Yii::app()->db->cache(1000, $dependency)->createCommand($sql)->queryAll();
  4. // query caching will NOT be used
  5. $rows = Yii::app()->db->createCommand($sql)->queryAll();


通过传递另一个参数$queryCount到cache()的方法中,我们可以强制多次查询缓存。下面的例子中,通过调用call(),我们指定这个换成必须用于接下来的2次

  1. // ...
  2. $rows = Yii::app()->db->cache(1000, $dependency, 2)->createCommand($sql)->queryAll();
  3. // query caching WILL be used
  4. $rows = Yii::app()->db->createCommand($sql)->queryAll();


如你所知,当我们执行关联AR查询时,可能是执行多句的SQL。例如,Post与Coment之间的关系是HAS_MANY,以下的SQL语句是数据库中真正执行的。

  1. it first selects the posts limited by 20; 
  2. it then selects the comments for the previously selected posts.
  3. $posts = Post::model()->with('comments')->findAll(array(
  4. 'limit'=>20,
  5. ));


如果我们用下面的语句查询缓存,只有第一句会被缓存:

  1. $posts = Post::model()->cache(1000, $dependency)->with('comments')->findAll(array(
  2.     'limit'=>20,
  3. ));


如果要缓存两个,我们要提供额外的参数来指明接下来我们要缓存几句:

  1. $posts = Post::model()->cache(1000, $dependency, 2)->with('comments')->findAll(array(
  2.     'limit'=>20,
  3. ));


限制

如果查询结果中包含资源句柄,查询访问就不能用了。例如,我们在某些数据库系统中使用BLOB作为字段类型,查询结果会返回一个资源句柄给这个字段。

有些缓存器会有大小限制。例如mencache限制每个入口大小为1M,所以,当一个查询结果大于该大小时,会缓存失败。

二、缓存的运用

提示 : 因为所有这些缓存组件都从同一个基础类 CCache 扩展而来,不需要修改使用缓存的代码即可在不同的缓存组
件之间切换。

缓存可以在不同的级别使用。在最低级别,我们使用缓存来存储单个数据,比如一个变量,我们把它叫做 数据缓存 。
往上一级,我们缓存一个由视图脚本生成的页面片断。在最高级别,我们存储整个页面以便需要的时候直接从缓存读
取。
接下来我们将阐述如何在这些级别上使用缓存。
注意 : 按定义来讲 , 缓存是一个不稳定的存储媒介 , 它不保证缓存一定存在 —— 不管该缓存是否过期 。 所以 , 不要使用
缓存进行持久存储(比如,不要使用缓存来存储 SESSION 数据 ) 。

一、数据缓存
数据缓存也就是在缓存中存储一些 PHP 变量 , 过一会再取出来 。 缓存基础类 CCache 提供了两个最常用的方法 : set()
和 get() 。
要在缓存中存储变量 $value ,我们选择一个唯一 ID 并调用 set() 来存储它:
Yii::app()->cache->set($id, $value);
被缓存的数据会一直保留在缓存中 , 直到因一些缓存策略而被删除 ( 比如缓存空间满了 , 删除最旧的数据 ) 。 要改变这
一行为,我们还可以在调用 set() 时加一个过期参数,这样数据过一段时间就会自动从缓存中清除。
// 在缓存中保留该值最多 30 秒
Yii::app()->cache->set($id, $value, 30);
当我们稍后需要访问该变量时 ( 不管是不是同一 Web 请求 ) , 我们调用 get() ( 传入 ID ) 来从缓存中获取它 。 如果返
回值为 false ,说明该缓存不可用,需要我们重新生成它。

  1. $value=Yii::app()->cache->get($id);
  2. if($value===false)
  3. {
  4. // 因为在缓存中没找到,重新生成 $value
  5. // 再缓存一下以备下次使用
  6. // Yii::app()->cache->set($id,$value);
  7. }


为一个要缓存的变量选择 ID 时,确保该 ID 在应用中是唯一的。不必保证 ID 在跨应用的情况下保证唯一,因为缓
存组件有足够的智能来区分不同应用的缓存 ID 。
要从缓存中删除一个缓存值 , 调用 delete() ; 要清空所有缓存 , 调用 flush() 。 调用 flush() 时要非常小心 , 因为它会把
其它应用的缓存也清空。
提示 : 因为 CCache 实现了 ArrayAccess 接口,可以像数组一样使用缓存组件。例如:

  1. $cache=Yii::app()->cache;
  2. $cache['var1']=$value1; // 相当于 : $cache->set('var1',$value1);
  3. $value2=$cache['var2']; // 相当于 : $value2=$cache->get('var2');


缓存依赖
除了过期设置,缓存数据还会因某些依赖条件发生改变而失效。如果我们缓存了某文件的内容,而该文件后来又被更
新了,我们应该让缓存中的拷贝失效,从文件中读取最新内容(而不是从缓存 ) 。
我们把一个依赖关系表现为一个 CCacheDependency 或它的子类的实例,调用 set() 的时候把依赖实例和要缓存的数
据一起传入。
// 缓存将在 30 秒后过期
// 也可能因依赖的文件有更新而更快失效
Yii::app()->cache->set($id, $value, 30, new CFileCacheDependency(‘FileName’));
如果我们现在调用 get() 从缓存中获取 $value ,缓存组件将检查依赖条件。如果有变,我们会得到 false 值 —— 数据
需要重新生成。
下面是可用的缓存依赖的简要说明:
CFileCacheDependency: 该依赖因文件的最近修改时间发生改变而改变。
CDirectoryCacheDependency: 该依赖因目录(或其子目录)下的任何文件发生改变而改变。
CDbCacheDependency: 该依赖因指定的 SQL 语句的查询结果发生改变而改变。
CGlobalStateCacheDependency: 该依赖因指定的全局状态值发生改变而改变。全局状态是应用中跨请求、跨 SESSION
的持久变量,它由 CApplication::setGlobalState() 来定义。
CChainedCacheDependency: 该依赖因依赖链中的任何一环发生改变而改变。

二、片段缓存   (Fragment Caching)
片段缓存指缓存网页某片段。例如,如果一个页面在表中显示每年的销售摘要,我们可以存储此表在缓存中,减少每
次请求需要重新产生的时间。
要使用片段缓存,在控制器视图脚本中调用 CController::beginCache() 和 CController::endCache() 。这两种方法开始和
结束包括的页面内容将被缓存。类似 data caching ,我们需要一个编号,识别被缓存的片段。
… 别的 HTML 内容 …

  1. <?php if($this->beginCache($id)) { ?>
  2. ... 被缓存的内容 ...
  3. <?php $this->endCache(); } ?>
  4. ... 别的 HTML 内容 ...


在上面的,如果 beginCache() 返回 false ,缓存的内容将此地方自动插入 ; 否则,在 if 语句内的内容将被执行并 在
endCache() 触发时缓存。

例如:

  1. <?php if($this->beginCache('tagCloud', array('duration'=>60))) { ?>
  2. <?php $this->widget('TagCloud'); ?>
  3. <?php $this->endCache(); } ?>
  4. <?php if($this->beginCache('RecentPosts', array('duration'=>60))) { ?>
  5. <?php $this->widget('RecentPosts'); ?>
  6. <?php $this->endCache(); } ?>
  7. <?php if($this->beginCache('RecentComments', array('duration'=>60))) { ?>
  8. <?php $this->widget('RecentComments'); ?>
  9. <?php $this->endCache(); } ?


1. 缓存选项 (Caching Options)
当调用 beginCache() ,可以提供一个数组由缓存选项组成的作为第二个参数,以自定义片段缓存。事实上为了方便,
beginCache() 和 endCache() 方法是 COutputCache widget 的包装。因此 COutputCache 的所有属性都可以在缓存选项中
初始化。

2. 有效期( Duration )
也许是最常见的选项是 duration , 指定了内容在缓存中多久有效 。 和 CCache::set() 过期参数有点类似 。 下面的代码缓存
内容片段最多一小时:

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


如果我们不设定期限,它将默认为 60 ,这意味着 60 秒后缓存内容将无效。

3. 依赖 (Dependency)
像 data caching ,内容片段被缓存也可以有依赖。例如,文章的内容被显示取决于文章是否被修改。
要指定一个依赖 , 我们建立了 dependency 选项 , 可以是一个实现 ICacheDependency 的对象或可用于生成依赖对象的配
置数组。下面的代码指定片段内容取决 lastModified 列的值是否变化:
… 其他 HTML 内容 …

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


4. 变化 (Variation)
缓存的内容可根据一些参数变化。例如,每个人的档案都不一样。缓存的档案内容将根据每个人 ID 变化。这意味着 ,
当调用 beginCache() 时将用不同的 ID 。
COutputCache 内置了这一特征,程序员不需要编写根据 ID 变动内容的模式。以下是摘要。
varyByRoute: 设置此选项为 true ,缓存的内容将根据 route 变化。因此,每个控制器和行动的组合将有一个单独的缓
存内容。
varyBySession: 设置此选项为 true , 缓存的内容将根据 session ID 变化 。 因此 , 每个用户会话可能会看到由缓存提供的
不同内容。
varyByParam: 设置此选项的数组里的名字 , 缓存的内容将根据 GET 参数的值变动 。 例如 , 如果一个页面显示文章的内
容根据 id 的 GET 参数,我们可以指定 varyByParam 为 array(‘id’) ,以使我们能够缓存每篇文章内容。如果没有这样的
变化,我们只能能够缓存某一文章。

这里有问题要讨论:
我可以正确使用以下数据调用

  1. if($this->beginCache('good_list_manager',array('duration'=>3600,'varyByParam'=>array('id')))){?>
  2. {
  3.     // ... display the content to be cached here
  4.    $this->endCache();
  5. }


但是以下代码不能使用,因为其参数为‘Goods[name]’YII无法正确识别!

  1. if($this->beginCache('good_list_manager',array('duration'=>3600,'varyByParam'=>array("Goods[name]")))
  2. {
  3.     // ... display the content to be cached here
  4.    $this->endCache();
  5. }


解决方法:自己建立根据 ID 变动内容的模式。

  1. if($this->beginCache('good_list_manager_'.$_GET[Goods][name].'_'.$_GET[Goods][bn].'_'.$_GET[Goods][cat_id].'_'.$_GET[Goods][brand_id].'_'.$_GET[Goods][marketable].'_'.$_GET[Goods][cps_status].'_'.$_GET[Goods][is_check],array('duration'=>3600))){?>
  2. {
  3.     // ... display the content to be cached here
  4.    $this->endCache();
  5. }


5. 请求类型(Request Types)
有时候,我们希望片段缓存只对某些类型的请求启用。例如,对于某张网页上显示表单,我们只想要缓存 initially
requested 表单 ( 通过 GET 请求 ) 。任何随后显示(通过 POST 请求)的表单将不被缓存,因为表单可能包含用户输入。
要做到这一点,我们可以指定 requestTypes 选项:

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


6. 嵌套缓存 (Nested Caching)
片段缓存可以嵌套。就是说一个缓存片段附在一个更大的片段缓存里。例如,意见缓存在内部片段缓存,而且它们一
起在外部缓存中在文章内容里缓存。

  1. ... 其他 HTML 内容 ...
  2. <?php if($this->beginCache($id1)) { ?>
  3. ... 外部被缓存内容 ...
  4. <?php if($this->beginCache($id2)) { ?>
  5. ... 内部被缓存内容 ...
  6. <?php $this->endCache(); } ?>
  7. ... 外部被缓存内容 ...
  8. <?php $this->endCache(); } ?>
  9. ... 其他 HTML 内容 ...


嵌套缓存可以设定不同的缓存选项。例如, 在上面的例子中内部缓存和外部缓存可以设置时间长短不同的持续值 。 当
数据存储在外部缓存无效 , 内部缓存仍然可以提供有效的内部片段 。 然而 , 反之就不行了 。 如果外部缓存包含有效的
数据, 它会永远保持缓存副本,即使内容中的内部缓存已经过期。

三、页面缓存
页面缓存指的是缓存整个页面的内容。页面缓存可以发生在不同的地方。例如,通过选择适当的页面头,客户端的浏
览器可能会缓存网页浏览有限时间。 Web 应用程序本身也可以在缓存中存储网页内容。 在本节中,我们侧重于后一
种办法。
页面缓存可以被看作是 片段缓存 (/doc/guide/caching.fragment) 一个特殊情况 。 由于网页内容是往往通过应用布局来生
成,如果我们只是简单的在布局中调用 beginCache() 和 endCache() ,将无法正常工作。这是因为布局 在
CController::render() 方法里的加载是在页面内容产生之后。
缓存整个页面,我们应该跳过产生网页内容的动作执行。我们可以使用 COutputCache 作为动作 过滤器
( /doc/guide/basics.controller#filter )来完成这一任务。下面的代码演示如何配置缓存过滤器:

  1. public function filters()
  2. {
  3. return array(
  4. array(
  5. 'system.web.widgets.COutputCache',
  6. 'duration'=>100,
  7. 'varyByParam'=>array('id'),
  8. ),
  9. );
  10. }


上述过滤器配置会使过滤器适用于控制器中的所有行动。我们可能会限制它在一个或几个行动通过使用插件操作器。
更多的细节中可以看过滤器( /doc/guide/basics.controller#filter ) 。
提示 : 我们可以使用 COutputCache 作为一个过滤器 , 因为它从 CFilterWidget 继承过来 , 这意味着它是一个工具 (widget)
和一个过滤器。事实上, widge 的工作方式和过滤器非常相似:工具 widget ( 过滤器 filter) 是在 action 动作里的内容执
行前执行,在执行后结束。

四、动态内容(Dynamic Content)
当使用 fragment caching 或 page caching , 我们常常遇到的这样的情况整个部分的输出除了个别地方都是静态的 。 例如 ,
帮助页可能会显示静态的帮助信息,而用户名称显示的是当前用户的。
解决这个问题,我们可以根据用户名匹配缓存内容,但是这将是我们宝贵空间一个巨大的浪费,因为缓存除了用户名
其他大部分内容是相同的。我们还可以把网页切成几个片段并分别缓存,但这种情况会使页面和代码变得非常复杂。
更好的方法是使用由 CController 提供的动态内容 dynamic content 功能 。
动态内容是指片段输出即使是在片段缓存包括的内容中也不会被缓存。即使是包括的内容是从缓存中取出,为了使动
态内容在所有时间是动态的,每次都得重新生成。出于这个原因,我们要求动态内容通过一些方法或函数生成。
调用 CController::renderDynamic() 在你想的地方插入动态内容。

  1. ... 别的 HTML 内容 ...
  2. <?php if($this->beginCache($id)) { ?>
  3. ... 被缓存的片段内容 ...
  4. <?php $this->renderDynamic($callback); ?>
  5. ... 被缓存的片段内容 ...
  6. <?php $this->endCache(); } ?>
  7. ... 别的 HTML 内容 ...


renderDynamic复杂调用形式:

  1. $this->renderDynamic(‘widget’,'application.extensions.uinfo’,array(‘uid’=>’hahahah!’), true); 
  2. 就相当于 
  3. $this->widget(‘application.extensions.uinfo’,array(‘uid’=>’hahahah!’), true);


在上面的, $callback 指的是有效的 PHP 回调。它可以是指向当前控制器类的方法或者全局函数的字符串名。它也可
以是一个数组名指向一个类的方法。其他任何的参数,将传递到 renderDynamic() 方法中。回调将返回动态内容而不是
仅仅显示它。
三、缓存注意事项:
请小心使用缓存,因为它增加了应用程序的性能,但降低了交互性。 (Please be careful to use the cache, because it increases the performance of the application, but decreases the interactivity.)

四、关于cacheKey的一些命名:

(1) CCacheHttpSession 中cacheKey的命名:

  1. /**
  2.          * Prefix to the keys for storing cached data
  3.          */
  4.         const CACHE_KEY_PREFIX='Yii.CCacheHttpSession.';
  5. /**
  6.          * Generates a unique key used for storing session data in cache.
  7.          * @param string $id session variable name
  8.          * @return string a safe cache key associated with the session variable name
  9.          */
  10.         protected function calculateKey($id)
  11.         {
  12.             return self::CACHE_KEY_PREFIX.$id;
  13.         }


使用:

  1. /**
  2.          * Session read handler.
  3.          * Do not call this method directly.
  4.          * @param string $id session ID
  5.          * @return string the session data
  6.          */
  7.         public function readSession($id)
  8.         {
  9.                 $data=$this->_cache->get($this->calculateKey($id));
  10.                 return $data===false?'':$data;
  11.         }


(2) COutputCache 中cacheKey的命名:

  1. const CACHE_KEY_PREFIX='Yii.COutputCache.';
  2. /**
  3.          * Caclulates the base cache key.
  4.          * The calculated key will be further variated in {@link getCacheKey}.
  5.          * Derived classes may override this method if more variations are needed.
  6.          * @return string basic cache key without variations
  7.          */
  8.         protected function getBaseCacheKey()
  9.         {
  10.                 return self::CACHE_KEY_PREFIX.$this->getId().'.';
  11.         }
  12.         /**
  13.          * Calculates the cache key.
  14.          * The key is calculated based on {@link getBaseCacheKey} and other factors, including
  15.          * {@link varyByRoute}, {@link varyByParam} and {@link varyBySession}.
  16.          * @return string cache key
  17.          */
  18.         protected function getCacheKey()
  19.         {
  20.                 if($this->_key!==null)
  21.                         return $this->_key;
  22.                 else
  23.                 {
  24.                         $key=$this->getBaseCacheKey().'.';
  25.                         if($this->varyByRoute)
  26.                         {
  27.                                 $controller=$this->getController();
  28.                                 $key.=$controller->getUniqueId().'/';
  29.                                 if(($action=$controller->getAction())!==null)
  30.                                         $key.=$action->getId();
  31.                         }
  32.                         $key.='.';
  33.                         if($this->varyBySession)
  34.                                 $key.=Yii::app()->getSession()->getSessionID();
  35.                         $key.='.';
  36.                         if(is_array($this->varyByParam) && isset($this->varyByParam[0]))
  37.                         {
  38.                                 $params=array();
  39.                                 foreach($this->varyByParam as $name)
  40.                                 {
  41.                                         if(isset($_GET[$name]))
  42.                                                 $params[$name]=$_GET[$name];
  43.                                         else
  44.                                                 $params[$name]='';
  45.                                 }
  46.                                 $key.=serialize($params);
  47.                         }
  48.                         $key.='.';
  49.                         if($this->varyByExpression!==null)
  50.                                 $key.=$this->evaluateExpression($this->varyByExpression);
  51.                         $key.='.';
  52.                         return $this->_key=$key;
  53.                 }
  54.         }


(3) CStatePersister 中cacheKey的命名:

  1. //从持久存储加载状态数据。
  2. public function load()
  3. {
  4. $stateFile=$this->stateFile;
  5. if($this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
  6. {
  7. $cacheKey='Yii.CStatePersister.'.$stateFile; //路径名加文件名
  8. if(($value=$cache->get($cacheKey))!==false)
  9. return unserialize($value);
  10. else if(($content=@file_get_contents($stateFile))!==false)
  11. {
  12. $cache->set($cacheKey,$content,0,new CFileCacheDependency($stateFile));
  13. return unserialize($content);
  14. }
  15. else
  16. return null;
  17. }
  18. else if(($content=@file_get_contents($stateFile))!==false)
  19. return unserialize($content);
  20. else
  21. return null;
  22. } 
  23. //保存应用程序状态到持久存储。
  24. public function save($state)
  25. {
  26. file_put_contents($this->stateFile,serialize($state),LOCK_EX);
  27. }

五、关于各个组件中用到的cache部分:

(1)CUrlManager 中

  1. const CACHE_KEY='Yii.CUrlManager.rules'; 
  2. public $cacheID='cache';
  3. /**
  4.          * Processes the URL rules.
  5.          */
  6.         protected function processRules()
  7.         {
  8.                 if(empty($this->rules) || $this->getUrlFormat()===self::GET_FORMAT)
  9.                         return;
  10.                 if($this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
  11.                 {
  12.                         $hash=md5(serialize($this->rules)); //这里写的很巧妙,用md5生成一个固定的字符串来保存rules,相当于一个缓存依赖中的 generateDependentData() 方法,通过它来判断是规则变了,用md5比较起来好比较
  13. //如果缓存存在,并且缓存依赖检查通过($data[1]===$hash),则返回缓存中存储的rules($data[0])
  14.                         if(($data=$cache->get(self::CACHE_KEY))!==false && isset($data[1]) && $data[1]===$hash)
  15.                         {
  16.                                 $this->_rules=$data[0];
  17.                                 return;
  18.                         }
  19.                 }
  20.                 foreach($this->rules as $pattern=>$route)
  21.                         $this->_rules[]=$this->createUrlRule($route,$pattern);
  22.                 if(isset($cache))
  23.                         $cache->set(self::CACHE_KEY,array($this->_rules,$hash)); //加$hash是为了下次访问时判断是否该rules变化过没,相当于CCacheDependency中的 evaluateDependency()来判断是否变化
  24.         }


五、关于CDummyCache :
CDummyCache是一个占位缓存组件。 他里面的方法和cache中的方法一样,不过是把设置缓存的代码给取消了,只返回操作成功后的结果,无实际操作代码。
CDummyCache不缓存任何数据。它使得我们总是可以配置一个缓存应用组件而不必检查Yii::app()->cache是否为null。将CDummyCache替换为其他缓存组件,可以快速的在无缓存模式和缓存模式之间切换。
测试阶段可以用他关闭缓存,设置如下:
//缓存配置

  1. 'cache' => array(
  2. 'class' => 'CDummyCache',
  3. ),


六、关于COutputCache:
想弄清CoutputCache,不得不知CController中的相关方法:
CController用到的关于cache的内容有:

  1. private $_cachingStack; //存储着关于各个CoutputCache的一个栈
  2. /**
  3. * Renders dynamic content returned by the specified callback.
  4. * This method is used together with {@link COutputCache}. Dynamic contents
  5. * will always show as their latest state even if the content surrounding them is being cached.
  6. * This is especially useful when caching pages that are mostly static but contain some small
  7. * dynamic regions, such as username or current time.
  8. * We can use this method to render these dynamic regions to ensure they are always up-to-date.
  9. *(输出一个由特殊回调函数返回的动态内容,这个方法被CoutputCache调用,动态内容始终保持最新的数据,尽管他所在的内容
  10. 被缓存了。)
  11. * The first parameter to this method should be a valid PHP callback, while the rest parameters
  12. * will be passed to the callback. (这个函数的第一个参数是一个回调函数,其他的参数会作为参数传递给这个回调函数)
  13. *
  14. * Note, the callback and its parameter values will be serialized and saved in cache.
  15. * Make sure they are serializable. (注意:回调函数和传递给回调函数的参数将要被序列化存储到cache中,所以,
  16. 请确保他们是可序列化的)
  17. *
  18. * @param callback $callback a PHP callback which returns the needed dynamic content.
  19. * When the callback is specified as a string, it will be first assumed to be a method of 
  20. the current
  21. * controller class. If the method does not exist, it is assumed to be a global PHP function.
  22. (如果这个回调函数是字符串,他会假设这个函数是当前控制器里的一个方法,如果这个方法不存在,就假设这个这个方法是个全局的
  23. 函数)
  24. * Note, the callback should return the dynamic content instead of echoing it.
  25. (这个回调函数应该返回动态的内容,而不是输出他)
  26. */
  27. public function renderDynamic($callback)
  28. {
  29. $n=count($this->_dynamicOutput);
  30. echo "<###dynamic-$n###>";
  31. $params=func_get_args();
  32. array_shift($params);
  33. $this->renderDynamicInternal($callback,$params);
  34. }
  35. /**
  36. * This method is internally used. (通过回调函数调用把处理结果放到_dynamicOutput数组中)
  37. * @param callback $callback a PHP callback which returns the needed dynamic content.
  38. * @param array $params parameters passed to the PHP callback
  39. * @see renderDynamic
  40. */
  41. public function renderDynamicInternal($callback,$params)
  42. {
  43. $this->recordCachingAction('','renderDynamicInternal',array($callback,$params));
  44. if(is_string($callback) && method_exists($this,$callback))
  45. $callback=array($this,$callback);
  46. $this->_dynamicOutput[]=call_user_func_array($callback,$params);
  47. }
  48. /**
  49. * Records a method call when an output cache is in effect.
  50. * When the content is served from the output cache, the recorded
  51. * method will be re-invoked.
  52. * @param string $context a property name of the controller. It refers to an object
  53. * whose method is being called. If empty it means the controller itself.(需要记录的方法
  54. 所在的对象,如果为空,则默认为当前的控制器这个对象)
  55. * @param string $method the method name (需要记录的方法)
  56. * @param array $params parameters passed to the method (传递给这个方法的参数)
  57. * @see COutputCache
  58. */
  59. public function recordCachingAction($context,$method,$params)
  60. {
  61. if($this->_cachingStack) // record only when there is an active output cache
  62. {
  63. foreach($this->_cachingStack as $cache)
  64. $cache->recordAction($context,$method,$params);
  65. }
  66. }
  67. /**
  68. * Postprocesses the dynamic output. (处理$output中插入的动态内容,处理过的内容是占位符被替换了真是的内容)
  69. * This method is internally used. Do not call this method directly.
  70. * @param string $output output to be processed
  71. * @return string the processed output
  72. */
  73. public function processDynamicOutput($output)
  74. {
  75. if($this->_dynamicOutput)
  76. {
  77.         //调用replaceDynamicOutput来处理每个匹配
  78. $output=preg_replace_callback('/<###dynamic-(\d+)###>/',array($this,'replaceDynamicOutput'),$output);
  79. }
  80. return $output;
  81. }
  82. /**
  83. * Replaces the dynamic content placeholders with actual content.
  84. * This is a callback function used internally. (替换匹配到的每个动态内容占位符,由processDynamicOutput
  85. 调用)
  86. * @param array $matches matches
  87. * @return string the replacement
  88. * @see processOutput
  89. */
  90. protected function replaceDynamicOutput($matches)
  91. {
  92. $content=$matches[0];  //所有的内容
  93. if(isset($this->_dynamicOutput[$matches[1]]))
  94. {
  95. $content=$this->_dynamicOutput[$matches[1]];
  96. $this->_dynamicOutput[$matches[1]]=null;
  97. }
  98. return $content;
  99. }
  100. /**
  101. * @param boolean $createIfNull whether to create a stack if it does not exist yet. Defaults to 
  102. true. (创建一个存储COutputCache对象的栈)
  103. * @return CStack stack of {@link COutputCache} objects
  104. */
  105. public function getCachingStack($createIfNull=true)
  106. {
  107. if(!$this->_cachingStack)
  108. $this->_cachingStack=new CStack;
  109. return $this->_cachingStack;
  110. }
  111. /**
  112. * Returns whether the caching stack is empty. (判断存储CoutputCache对象的栈是否为空)
  113. * @return boolean whether the caching stack is empty. If not empty, it means currently there are
  114. * some output cache in effect. Note, the return result of this method may change when it is
  115. * called in different output regions, depending on the partition of output caches.
  116. */
  117. public function isCachingStackEmpty()
  118. {
  119. return $this->_cachingStack===null || !$this->_cachingStack->getCount();
  120. }
  121. /**
  122. * Postprocesses the output generated by {@link render()}.
  123. * This method is invoked at the end of {@link render()} and {@link renderText()}.
  124. * If there are registered client scripts, this method will insert them into the output
  125. * at appropriate places. If there are dynamic contents, they will also be inserted.
  126. * This method may also save the persistent page states in hidden fields of
  127. * stateful forms in the page.
  128. * @param string $output the output generated by the current action
  129. * @return string the output that has been processed.
  130. */
  131. public function processOutput($output)
  132. {
  133. Yii::app()->getClientScript()->render($output);  //插入静态的js,css文件等
  134. // if using page caching, we should delay dynamic output replacement
  135. if($this->_dynamicOutput!==null && $this->isCachingStackEmpty())
  136. {
  137. $output=$this->processDynamicOutput($output);
  138. $this->_dynamicOutput=null;
  139. }
  140. if($this->_pageStates===null)
  141. $this->_pageStates=$this->loadPageStates();
  142. if(!empty($this->_pageStates))
  143. $this->savePageStates($this->_pageStates,$output);
  144. return $output;
  145. }