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
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.
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.
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.
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.
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);
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With