2016-02-09 71 views
10

我嘗試緩存顯示菜單的塊(例如來自Cmssmart_megamenu的模塊)。Magento緩存 - 規則清理緩存

以前的版本是:

<block type="megamenu/navigation" name="catalog.topnav.megamenu"> 
    <action method="unsetData"><key>cache_lifetime</key></action> 
    <action method="unsetData"><key>cache_tags</key></action> 
</block> 

所以筆者在explicetly禁用緩存。 我刪除了2個unsetData,並在Cmsmart_Megamenu_Block_Navigation類中添加了_construct()方法。

class Cmsmart_Megamenu_Block_Navigation extends Mage_Catalog_Block_Navigation 
{ 

protected function _construct() 
    $this->addData(array(
    'cache_lifetime' => 86400, 
    'cache_key'  => "my_key_mega_menu", 
    'cache_tags'  => array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Cms_Model_Block::CACHE_TAG) 
)); 

聽起來好像有效,我可以看到緩存文件:mage --- 8ea_MY_KEY_MEGA_MENU。 var/cache中的 。 但是,它會在一分鐘之內從緩存中消失。實際上只要下一個cron開始(每mn計劃一次)

我使用了Aoe-template_hint,我可以看到一個綠色的框,表示它被緩存了,同時生命週期也被正確設置爲86400,所以怎麼了?

這是我第一次嘗試,你認爲這裏有什麼錯誤? 是否有其他規則,而不是文件的過期時間?也許有一個與另一個塊更快過期的隱藏鏈接? 一個小於1MN緩存奇怪反正...

注:我在Windows或Linux的同樣的問題,並有或無的Redis

感謝

+0

您是否嘗試過使用完全組成的緩存標記?像藍色字符串'my_own_cache_tag'那樣的東西只是爲了確保沒有cron根據您正在使用的核心緩存標記進行清理? –

+0

是的,我做過,同樣適用 – Rod

+0

必須嘗試覆蓋'public function toHtml()'並添加'echo'cache_lifetime:'。 $這 - > getCacheLifetime();返回父:: toHtml();'在那裏?有兩件事情應該發生:如果緩存是冷的(不是創建的),它應該回顯你在構造中指定的cache_lifetime,如果緩存是構建的(溫暖的),它不應該回應你任何cache_lifetime。 –

回答

1

經過更多的調查,我發現爲什麼我的塊的緩存被刪除。

我搜索使用者撥打清理緩存的方法,我發現它的模塊實際上是明確刪除所有緩存在每個cron的阻斷這樣做是因爲:

Mage::app()->getCacheInstance()->cleanType('block_html'); 

我刪除了行,現在它進展順利! 該模塊是async_index

2

一個原因可能是,如果你實際上在你自己的_construct的末尾做了parent::_construct(),你沒有向我們展示。

因爲我的版本的Magento(22年9月1日),我在Mage_Catalog_Block_Navigation_construct看到Magento的是這樣做的:

protected function _construct() 
{ 
    $this->addData(array('cache_lifetime' => false)); 
    $this->addCacheTag(array(
     Mage_Catalog_Model_Category::CACHE_TAG, 
     Mage_Core_Model_Store_Group::CACHE_TAG 
    )); 
} 

所以該行$this->addData(array('cache_lifetime' => false));只會覆蓋您的設置。

解決那麼這將是parent::_construct()第一然後加上自己的cache_lifetime

這樣的方式:

protected function _construct(){ 
parent::_construct(); // that calls the parent, then you override the cache_lifetime 

    $this->addData(array(
    'cache_lifetime' => 86400, 
    'cache_key'  => "my_key_mega_menu", 
    'cache_tags'  => array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Cms_Model_Block::CACHE_TAG) 
)); 

    // parent::_construct(); but if you have it there, it will cause issues because this will override your settings 
} 

另一個路徑可以是隻覆蓋的方法,以獲取有關這些信息緩存,現在使用神奇的吸氣劑Varien_Object並做類似的事情:

/* That is not even needed anymore 
protected function _construct(){ 
    $this->addData(array(
    'cache_lifetime' => 86400, 
    'cache_key'  => "my_key_mega_menu", 
    'cache_tags'  => array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Cms_Model_Block::CACHE_TAG) 
)); 
}*/ 

public function getCacheLifetime() { 
    return 86400; 
} 

public function getCacheKey() { 
    return 'my_key_mega_menu'; 
} 

public function getCacheTags() { 
    return array(Mage_Catalog_Model_Category::CACHE_TAG, Mage_Cms_Model_Block::CACHE_TAG); 
}