cancel
Showing results for 
Search instead for 
Did you mean: 

Cache for your own blocks or widgets

Cache for your own blocks or widgets

In Magento, custom blocks that are extended from Magento\Framework\View\Element\Template are not automatically cached. You must explicitly enable and configure caching for each custom block or widget. If you want to enable caching for a block, you must implement the getCacheLifetime, getCacheKey and getCacheTags methods in your block class. These methods define the cache lifetime, cache key and cache tags for the block.

Is that right? I thought that if a block class is extended from Magento\Framework\View\Element\Template, this custom block or widget is also automatically cached. I thought I didn't have to do anything.

 

Vladimir

1 REPLY 1

Re: Cache for your own blocks or widgets

Hello @mailmirons1551 

 

You're correct in that Magento 2 automatically handles caching for blocks that extend the Magento\Framework\View\Element\Template class, but there are some nuances to understand. 

 

By default, the caching behavior works like this:

     Static content: If your block contains only static content (no dynamic data or logic), Magento will cache the output.

 

     Dynamic content: If your block fetches data dynamically (e.g., database queries, session data), Magento will still cache the output but might need additional cache keys or tags to ensure correct cache invalidation.

 

 

 

Hope it helps ! 

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

 

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9