View Caching
ada perubahan tertangguh untuk seksyen ini. More information about translations
Since 0.10.9.2378_final, Cake has support for view caching (also called Full Page Caching ). No, we are not kidding. You can now cache your layouts and views. You can also mark parts of your views to be ignored by the caching mechanism. The feature, when used wisely, can increase the speed of your app by a considerable amount.
When you request a URL, Cake first looks to see if the requested URL isn't already cached. If it is, Cake bypasses the dispatcher and returns the already rendered, cached version of the page. If the page isn't in the cache, Cake behaves normally.
If you've activated Cake's caching features, Cake will store the output of its normal operation in the cache for future user. The next time the page is requested, Cake will fetch it from the cache. Neat, eh? Let's dig in to see how it works.
How Does it Work ?
Activating the cache
By default, view caching is disabled. To activate it, you first need to change the value of CACHE_CHECK in /app/config/core.php from false to true:
/app/config/core.php (partial)
define ('CACHE_CHECK', true); define ('CACHE_CHECK', true);
This line tells Cake that you want to enable View Caching.
In the controller for the views you want to cache you have to add the Cache helper to the helpers array:
var $helpers = array('Cache'); var $helpers = array('Cache');
Next, you'll need to specify what you want to cache.
The $cacheAction Controller Variable
In this section, we will show you how to tell Cake what to cache. This is done by setting a controller variable called $cacheAction. The $cacheAction variable should be set to an array that contains the actions you want to be cached, and the time (in seconds) you want the cache to keep its data. The time value can also be a strtotime() friendly string (i.e. '1 day' or '60 seconds').
Let's say we had a ProductsController, with some things that we'd like to cache. The following examples show how to use $cacheAction to tell Cake to cache certain parts of the controller's actions.
$cacheAction Examples
Cache a few of the most oft visited product pages for six hours:
var $cacheAction = array(
'view/23/' => 21600,
'view/48/' => 21600
); 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); 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";
var $cacheAction = "1 hour";
You can also define caching in the actions using $this->cacheAction = array()
Marking Content in the View
There are instances where you might not want parts of a view cached. If you've got some sort of element highlighting new products, or something similar, you might want to tell Cake to cache the view... except for a small part.
You can tell Cake not to cache content in your views by wrapping <cake:nocache> </cake:nocache> tags around content you want the caching engine to skip.
<cake:nocache> example
<h1> New Products! </h1> <cake:nocache> <ul> <?php foreach($newProducts as $product): ?> <li>$product['name']</li> <?endforeach;?> </ul> </cake:nocache>
<h1> New Products! </h1><cake:nocache><ul><?php foreach($newProducts as $product): ?><li>$product['name']</li><?endforeach;?></ul></cake:nocache>
Clearing the cache
First, you should be aware that Cake will automatically clear the cache if a database change has been made. For example, if one of your views uses information from your Post model, and there has been an INSERT, UPDATE, or DELETE made to a Post, Cake will clear the cache for that view.
But there may be cases where you'll want to clear specific cache files yourself. To do that Cake provides the clearCache function, which is globally available:
clearCache example
//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 paramsclearCache('controller_action_params');//You can also use an array to clear muliple caches at once.clearCache(array('controller_action_params','controller2_action_params));
Things To Remember
Below are a few things to remember about View Caching:
-
To enable cache you set CACHE_CHECK to true in /app/config/core.php .
-
In the controller for the views you want to cache you have to add the Cache helper to the helpers array.
-
To cache certain URLs, use $cacheAction in the controller.
-
To stop certain parts of a view from being cached, wrap them with <cake:nocache> </cake:nocache>
-
Cake automatically clears specific cache copies when changes are made to the database
-
To manually clear parts of the cache, use clearCache().


























