Magento 2: Force block to be cached
-
12-12-2020 - |
Question
I've a mega menu extension which I feel is quite slow. It seems to add about 2 seconds on load time for each page.
As this menu does not change between pages I was wondering how to cache a block? I want it generated just once on the first-page load after a cache flush so that each page can just load the cached block rather than having to repeat for each page on site.
The pages are cached and am not talking about the FPC here as page load is fine when cached but over 15,000 odd pages warming up a cache will save roughly 30,000 seconds if this is not repeated constantly.
Solution
I think every block cache in magento,
But if you want to increase cachelifetime
of particular block
You can do from your extension's block
go to your block and write below method
this method already there in Magento\Framework\View\Element\AbstractBlock
protected function getCacheLifetime()
{
return 84600; // for one day you can increase or decrease this num as per your req
}
The cache lifetime can also be set like so within construct:
$this->setCacheLifetime(846000);