CakePHP视图缓存(View Caching)
View Caching
Section 1 视图(View)缓存
什么是View Cache
从0.10.9.2378_final版本开始,Cake开始支持视图缓存(译注:为了更好的表达,我改用“页面缓存”)。我们没有开玩笑,现在你可以缓存你的页面了,而且可以标记部分视图不被缓存。可以预见当大范围使用该功能时,你的应用程序速度将获得可观的提升。
当对指定URL发起请求时,Cake首先检查该URL是否已经被缓存。如果已经被缓存,Cake绕开分发器(dispatcher)直接返回已经输出并缓存起来的页面。反之,则走正常的流程来输出页面。
如果你已经激活了Cake的缓存机制,Cake会缓存输出的页面已备下次调用。当下一次请求该页面,Cake会从缓存中提取该页面并返回。你是不是在想就那么简单?好吧,让我们来看它是怎么来操作的。
Section 2 Cache是如何工作的
激活缓存
默认情况下,页面缓存机制是被禁用的。为了激活此功能,你首先需要在/app/config/core.php中将 CACHE_CHECK 的值设为true。
/app/config/core.php (partial)
define ('CACHE_CHECK', true);
这个变量决定了是否启用页面缓存机制
在你希望缓存的视图(View)对应的controller中包含Cache Helper:
var $helpers = array('Cache');
然后你需要指定哪些是需要被缓存的。
Controller中的 $cacheAction 变量
本小节中,我们介绍如何告诉Cake哪些是需要被缓存的。这由为controller中的 $cacheAction 变量的值决定。该变量是一个array,包含了所有希望被缓存的action名字和对应缓存的生命周期。这个时间的值可以是一个一个友好的日期字符串(例如:'1 day' 或者 '60 seconds')。
假设我们有一个ProductsController,其中有几个action希望被缓存。下面的示例代码告诉你应该如何告知Cake哪些该被缓存,注意,Cake的缓存是基于URL的:
$cacheAction Examples
//Cache a few of the most oft visited product pages for six hours:
var $cacheAction = array(
'view/23/' => 21600,
'view/48/' => 21600
);
//Cache an entire action. In this case the recalled product list, for one day:
var $cacheAction = array('recalled/' => 86400);
//If we wanted to, we could cache every action by setting it to a string:
//that is strtotime() friendly to indicate the caching time.
var $cacheAction = "1 hour";
//You can also define caching in the actions using $this->cacheAction = array()...
$cacheAction Examples
//Cache a few of the most oft visited product pages for six hours:
var $cacheAction = array(
'view/23/' => 21600,
'view/48/' => 21600
);
//Cache an entire action. In this case the recalled product list, for one day:
var $cacheAction = array('recalled/' => 86400);
//If we wanted to, we could cache every action by setting it to a string:
//that is strtotime() friendly to indicate the caching time.
var $cacheAction = "1 hour";
//You can also define caching in the actions using $this->cacheAction = array()...
视图中缓存标记
有的时候我们希望页面上的部分内容不被缓存。比如你希望新上架的货品能够得到高亮显示,或类似这种具有时间特性的内容,你可以告诉Cake不要缓存这部分内容。
而如何通知Cake呢,很简单,使用 将不希望缓存的内容包起来就可以了。
<h5><cake:nocache> example</h5>
<h1> New Products! </h1>
<cake:nocache>
<ul>
<?php foreach($newProducts as $product): ?>
<li>$product['name']</li>
<?endforeach;?>
</ul>
</cake:nocache>
<h5><cake:nocache> example</h5>
<h1> New Products! </h1>
<cake:nocache>
<ul>
<?php foreach($newProducts as $product): ?>
<li>$product['name']</li>
<?endforeach;?>
</ul>
</cake:nocache>
Clearing the cache 清空缓存
首先我们先要告诉你的是,当数据库发生变动时Cake会自动清空缓存。比如有一个视图的信息是从Post model获取的,假如Post model有了INSERT, UPDATE, DELETE操作后,Cake会自动清空该缓存。
但是有的时候可能会需要手动的去清空缓存。Cake为此提供了 clearCache 函数来执行该动作。该函数是一个全局的变量:
//Remove all cached pages that have the controller name.
clearCache('controller');
//Remove all cached pages that have the controller_action name.
clearCache('controller_action/');
//Remove all cached pages that have the controller_action_params name.
//Note: you can have multiple params
clearCache('controller_action_params');
//You can also use an array to clear muliple caches at once.
clearCache(array('controller_action_params','controller2_action_params));
//Remove all cached pages that have the controller name.
clearCache('controller');
//Remove all cached pages that have the controller_action name.
clearCache('controller_action/');
//Remove all cached pages that have the controller_action_params name.
//Note: you can have multiple params
clearCache('controller_action_params');
//You can also use an array to clear muliple caches at once.
clearCache(array('controller_action_params','controller2_action_params));
Section 3 需要记住的事情
下面是一些你需要记住的事情:
在/app/config/core.php中设置 CACHE_CHECK 为true。
对于希望缓存的Controller必须包含Cache Helper。
配置 $cacheAction 来缓存指定的URL。
使用<cake:nocache> </cake:nocache>来忽略页面上的部分内容不被缓存。
在数据发生变更时,Cake会自动清空相关联的缓存。
可以使用 clearCache() 函数来手动清空缓存。