I have a block file that does heavy data processing due to which my product page PHP rendering time increases above 2 seconds. I have disabled page_cache
in my Magento 2 system so i cannot use cacheable="true"
in my layout file that loads this block file with it's template. I am looking for a solution where i can cache this file code into block_html
cache type. Can anyone please tell me the solution on how i can implement this functionality.
My SampleClass
is below which is extending core Magento 2's \Magento\Catalog\Block\Product\View
class:
class SampleClass extends \Magento\Catalog\Block\Product\View
{
//rest of my code like __construct and other custom methods.
}
Best Answer
Finally got a solution for the above question. I have used two methods
getCacheLifetime()
andgetCacheKeyInfo()
of the core Magento 2 template block file and overridden those two methods of the class:Magento\Framework\View\Element\AbstractBlock
.The code looks like below: