2012-07-07 15 views
2

關於到cookbook我們可以緩存這樣的內容:CakePHP的緩存元素與持續時間

echo $this->element('helpbox', array(), array('cache' => true)); 

緩存與配置是這樣的:

echo $this->element('helpbox', array(), 
    array('cache' => array('config' => 'view_long')); 

我怎麼能不緩存預定義的配置元素?我如何緩存元素的持續時間?我想這一點,但沒有奏效:

echo $this->element('helpbox', array(), 
    array('cache' => array('time' => '+30 minutes'))); 

回答

2

您需要configure cacheapp/Config/bootstrap.php

Cache::config('hour', array(
    'engine' => 'File', 
    'duration' => '+1 hours', 
    'path' => CACHE, 
    'prefix' => 'cake_short_' 
)); 

Cache::config('week', array(
    'engine' => 'File', 
    'duration' => '+1 week', 
    'probability' => 100, 
    'path' => CACHE . 'long' . DS, 
)); 

在這之後,你可以使用定義的配置緩存你的元素:

echo $this->element('helpbox', array(), array('cache' => array('config' => 'week'))); 
+0

謝謝,這很好!最後一行需要多一個右括號。 – trante 2012-07-07 17:18:12

0

由於您現在只能引用命名緩存配置,如果要通過編程方式清除緩存元素,則需要使用Cache :: delete()和元素名稱和密鑰。

我寫了一個blog post這個。 the relevant CakePHP forum thread還有一些更多的細節。

(8/31/14)我還沒有檢查,如果這仍然是CakePHP 2.5中的行爲。

+0

感謝您發佈您的答案!請務必仔細閱讀[自助推廣常見問題](http://stackoverflow.com/faq#promotion)。另請注意,每次鏈接到您自己的網站/產品時,您都必須*發佈免責聲明。 – 2012-12-17 14:23:13