【问题标题】:Magento cache - Rule to Clean cacheMagento 缓存 - 清理缓存的规则
【发布时间】:2016-10-28 20:03:42
【问题描述】:

我尝试缓存显示菜单的块(例如来自 Cmssmart_megamenu 的模块)。

以前的版本是:

<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>

所以作者明确禁用缓存。 我删除了 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。 在变量/缓存中。 但是,它会在一分钟内从缓存中消失。实际上,只要下一个 cron 启动(每 mn 安排一次)

我使用了 Aoe-template_hint,我看到这个块有一个绿色框,表示它被缓存了,并且生命周期也正确设置为 86400,所以出了什么问题?

这是我的第一次尝试,您认为这里有什么问题? 除了文件过期的持续时间之外,还有其他规则吗?也许有一个隐藏链接与另一个更快过期的块? 无论如何,少于 100 万的缓存很奇怪...

注意:我在 Windows 或 Linux 上遇到同样的问题,无论是否使用 Redis

谢谢

【问题讨论】:

  • 您是否尝试过使用完全编造的缓存标签?像字符串'my_own_cache_tag'这样出乎意料的东西只是为了确保确实没有cron根据您在那里使用的核心缓存标签进行清理?
  • 是的,同样适用
  • 必须尝试覆盖public function toHtml() 并添加echo 'cache_lifetime is : ' . $this-&gt;getCacheLifetime(); return parent::toHtml(); 那里?应该发生两件事:如果缓存是冷的(未创建),它应该回显你在构造中指定的 cache_lifetime,如果缓存是构造的(暖的),它不应该回显任何 cache_lifetime。
  • 另外,你没有向我们展示真实的代码。你的 _construct 函数没有它的块代码,所以,问题是,你有一个 parent::_construct 吗?
  • 很高兴看到您解决了自己的问题。能否请您将您的编辑作为答案发布,然后接受您自己的答案作为好的答案。因此,如果没有公认的答案,您的问题不会一直存在。谢谢:)

标签: magento caching


【解决方案1】:

一个原因可能是,如果您实际上在自己的 _construct 末尾添加了 parent::_construct(),而您没有向我们展示。

因为在我的 Magento (1.9.22) 版本中,我在 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-&gt;addData(array('cache_lifetime' =&gt; 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 的魔法 getter 并执行以下操作:

/* 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);
}

【讨论】:

    【解决方案2】:

    经过更多调查,我发现了为什么我的块的缓存被删除了。

    我搜索了对清理缓存的方法的调用,我发现这是由于一个模块实际上是通过这样做显式删除每个 cron 处的所有缓存块:

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

    我删除了线路,现在一切正常! 该模块是 async_index

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 2011-05-12
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多