Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento cache - Rule to Clean cache

I try to cache the block that displays the menu (for instance a module from Cmssmart_megamenu).

The previous version was:

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

So the author was explicetly disabling cache. I removed the 2 unsetData, and added a _construct() method in Cmsmart_Megamenu_Block_Navigation class.

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

It sounds like it works, I can see the cache file: mage---8ea_MY_KEY_MEGA_MENU. in var/cache. However, it disappears from the cache after within a minute. Actually as soon as the next cron starts (it is scheduled every mn)

I used Aoe-template_hint and I can see a green box for this block, meaning it is cached, with also the lifetime set correctly to 86400, so what's wrong?

that's my first attempt, what do you think is wrong here? Is there other rule than the duration for a file to expire? Maybe there is an hidden link with another block that expire sooner? a cache of less than 1mn is strange anyways...

Note: I have the same issue on Windows or Linux, and with or without Redis

thanks

like image 728
Rod Avatar asked Feb 09 '16 19:02

Rod


People also ask

How do I clear the cache in Magento?

Go to System > Cache Management > Flush Cache Storage. Go to System > Tools > Cache Management. Select Flush Magento Cache and after the process is completed select Flush Cache Storage.

How do I fix cache in Magento 2?

To overcome these issues Magento features Static Content Signing which allows you to invalidate the browser cache. Flush JavaScript/CSS Cache after deployment if the changes are not reflecting. Static content signing: Magento feature that allows you to invalidate the browser cache for static resources.

What is the difference between cache clean and cache flush in Magento 2?

Hence, the main difference between Cache clean and cache flush in Magento 2 is that cache:clean will wipe all the enabled items which are Magento-related, and cache:flush wipes out the cache storage.

What does flush cache mean Magento?

Description. Flush Magento Cache. Removes all items in the default Commerce cache ( var/cache ), according to their associated Commerce tags. Flush Cache Storage. Removes all items from the cache, regardless of Commerce tag.


2 Answers

One reason could be, if you actually do a parent::_construct() at the end of your own _construct that you are not showing us.

Because on my version of Magento (1.9.22), I see in the _construct of Mage_Catalog_Block_Navigation that Magento is doing :

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

So that line $this->addData(array('cache_lifetime' => false)); will just override your set.

A way to solve it would then be to parent::_construct() first then add your own cache_lifetime

Like this :

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
}

Another path could be to just override the methods to get those informations about cache that are right now using the magic getter of Varien_Object and do something like :

/* 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);
}
like image 75
β.εηοιτ.βε Avatar answered Oct 20 '22 19:10

β.εηοιτ.βε


after more investigation, I found why the cache of my block was removed.

I searched for calls to methods that clean the cache and I found it was due to a module that was actually explicitly removing all caches block at every cron by doing this:

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

I removed the line, and it goes well now! The module was async_index

like image 23
Rod Avatar answered Oct 20 '22 20:10

Rod