Welcome to the Cookbook

loading...

{PT} - 7.2.4 Manipulando Cache no Controller

Qualquer controller que utilize a funcionalidade de cache precisa incluir o CacheHelper em seu array $helpers.

var $helpers = array('Cache');
  1. var $helpers = array('Cache');

Você também precisa indicar que actions precisam de cache, e como será feito o cache de cada action. Isto é feito por meio da variável $cacheAction em seus controllers. $cacheAction deve ser atribuída com um array que contenha as actions as quais você queira manter em cache e a duração, em segundos, indicando por quanto tempo você quer que as views permaneçam em cache. Este tempo pode ser informado também no mesmo formato que para o strtotime() (p.ex., "1 hour" ou "3 minutes").

Considere um exemplo de um ArticlesController, que recebe muito tráfego e que precisa de cache.

O código abaixo faz cache dos artigos frequentemente visitados por diferentes períodos de tempo.

var $cacheAction = array(
	'view/23' => 21600,
	'view/48' => 36000,
	'view/52'  => 48000
);
  1. var $cacheAction = array(
  2. 'view/23' => 21600,
  3. 'view/48' => 36000,
  4. 'view/52' => 48000
  5. );

Aqui, o cache de uma action completa, neste caso, uma listagem grande de artigos.

var $cacheAction = array(
	'archives/' => '60000'
);
  1. var $cacheAction = array(
  2. 'archives/' => '60000'
  3. );

E agora, um cache para cada action no controller usando o formato strtotime(), mais amigável, para indicar o período de duração do cache no controller.

var $cacheAction = "1 hour";
  1. var $cacheAction = "1 hour";

{EN} - 7.2.4 Caching in the Controller

Any controllers that utilize caching functionality need to include the CacheHelper in their $helpers array.

var $helpers = array('Cache');
  1. var $helpers = array('Cache');

You also need to indicate which actions need caching, and how long each action will be cached. This is done through the $cacheAction variable in your controllers. $cacheAction should be set to an array which contains the actions you want cached, and the duration in seconds you want those views cached. The time value can be expressed in a strtotime() format. (ie. "1 hour", or "3 minutes").

Using the example of an ArticlesController, that receives a lot of traffic that needs to be cached.

Cache frequently visited Articles for varying lengths of time

var $cacheAction = array(
	'view/23' => 21600,
	'view/48' => 36000,
	'view/52'  => 48000
);
  1. var $cacheAction = array(
  2. 'view/23' => 21600,
  3. 'view/48' => 36000,
  4. 'view/52' => 48000
  5. );

Remember to use your routes in the $cacheAction if you have any.

Cache an entire action in this case a large listing of articles

var $cacheAction = array(
	'archives/' => '60000'
);
  1. var $cacheAction = array(
  2. 'archives/' => '60000'
  3. );

Cache every action in the controller using a strtotime() friendly time to indicate Controller wide caching time.

var $cacheAction = "1 hour";
  1. var $cacheAction = "1 hour";

You can also enable controller/component callbacks for cached views created with CacheHelper. To do so you must use the array format for $cacheAction and create an array like the following:

var $cacheAction = array(
	'view' => array('callbacks' => true, 'duration' => 21600),
	'add' => array('callbacks' => true, 'duration' => 36000),
	'index'  => array('callbacks' => true, 'duration' => 48000)
);
  1. var $cacheAction = array(
  2. 'view' => array('callbacks' => true, 'duration' => 21600),
  3. 'add' => array('callbacks' => true, 'duration' => 36000),
  4. 'index' => array('callbacks' => true, 'duration' => 48000)
  5. );

By setting callbacks => true you tell CacheHelper that you want the generated files to create the components and models for the controller. As well as, fire the component initialize, controller beforeFilter, and component startup callbacks.

callbacks => true partly defeats the purpose of caching. This is also the reason it is disabled by default.

Diferenças

Lines: 1-35Lines: 1-24
-<title>Caching in the Controller</title>
<p>Any controllers that utilize caching functionality need to include the CacheHelper in their $helpers array.</p>
+<title>Manipulando Cache no Controller</title>
<p>Qualquer controller que utilize a funcionalidade de cache precisa incluir o CacheHelper em seu array $helpers.</p>
 <pre> <pre>
 var $helpers = array('Cache'); var $helpers = array('Cache');
 </pre> </pre>
-<p>You also need to indicate which actions need caching, and how long each action will be cached. This is done through the $cacheAction variable in your controllers. $cacheAction should be set to an array which contains the actions you want cached, and the duration in seconds you want those views cached. The time value can be expressed in a strtotime() format. (ie. "1 hour", or "3 minutes"). +<p>Vo também precisa indicar que actions precisam de cache, e como se feito o cache de cada action. Isto é feito por meio da variável $cacheAction em seus controllers. $cacheAction deve ser atribuída com um array que contenha as actions as quais você queira manter em cache e a duração, em segundos, indicando por quanto tempo você quer que as views permaneçam em cache. Este tempo pode ser informado também no mesmo formato que para o strtotime() (p.ex., "1 hour" ou "3 minutes").
 </p> </p>
-<p>Using the example of an ArticlesController, that receives a lot of traffic that needs to be cached.</p>
<p>Cache frequently visited Articles for varying lengths of time</p>
+<p>Considere um exemplo de um ArticlesController, que recebe muito tráfego e que precisa de cache.</p>
<p>O código abaixo faz cache dos artigos frequentemente visitados por diferentes períodos de tempo.</p>
 <pre> <pre>
 var $cacheAction = array( var $cacheAction = array(
  'view/23' =&gt; 21600,  'view/23' =&gt; 21600,
  'view/48' =&gt; 36000,  'view/48' =&gt; 36000,
  'view/52' =&gt; 48000  'view/52' =&gt; 48000
 ); );
 </pre> </pre>
-<p>Remember to use your routes in the $cacheAction if you have any.</p>
&
lt;p>Cache an entire action in this case a large listing of articles</p>
+<p>Aqui, o cache de uma action completa, neste caso, uma listagem grande de artigos.</p>
 <pre> <pre>
 var $cacheAction = array( var $cacheAction = array(
  'archives/' =&gt; '60000'  'archives/' =&gt; '60000'
 ); );
 </pre> </pre>
-<p>Cache every action in the controller using a strtotime() friendly time to indicate Controller wide caching time.</p> +<p>E agora, um cache para cada action no controller usando o formato strtotime(), mais amigável, para indicar o período de duração do cache no controller.</p>
 <pre>var $cacheAction = "1 hour";</pre> <pre>var $cacheAction = "1 hour";</pre>
-<p>You can also enable controller/component callbacks for cached views created with <code>CacheHelper</code>. To do so you must use the array format for <code>$cacheAction</code> and create an array like the following: 
-<pre> 
-var $cacheAction = array( 
- 'view' =&gt; array('callbacks' =&gt; true, 'duration' =&gt; 21600), 
- 'add' =&gt; array('callbacks' =&gt; true, 'duration' =&gt; 36000), 
- 'index' =&gt; array('callbacks' =&gt; true, 'duration' =&gt; 48000) 
-); 
-</pre> 
-<p>By setting <code>callbacks => true</code> you tell CacheHelper that you want the generated files to create the components and models for the controller. As well as, fire the component initialize, controller beforeFilter, and component startup callbacks.</p> 
-<p class="note"Setting <code>callbacks => true</code> partly defeats the purpose of caching. This is also the reason it is disabled by default.</p>