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

2 REPLIES 2

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

Re: Cache for your own blocks or widgets

Hello @mailmirons1551,

 

you're correct in questioning that assumption. In Magento 2, blocks that extend Magento\Framework\View\Element\Template are not automatically cached. Magento does not cache template blocks by default unless you explicitly enable caching for them.

 

If you want to cache a custom block, you must implement these methods in your block class:

  • getCacheLifetime() – Defines how long (in seconds) the block should be cached. If this method is missing or returns null, Magento will not cache the block.
  • getCacheKey() – Returns a unique cache key to identify this block's cached content.
  • getCacheTags() – Returns an array of cache tags, which helps Magento clear the cache when related content changes.

Here’s an example of a custom block with caching enabled:

namespace Your\Module\Block;



use Magento\Framework\View\Element\Template;



class CustomBlock extends Template

{

    /**

     * Get cache lifetime (in seconds)

     *

     * @return int|null

     */

    public function getCacheLifetime()

    {

        return 86400; // Cache for 24 hours

    }



    /**

     * Get cache key info

     *

     * @return array

     */

    public function getCacheKeyInfo()

    {

        $cacheKeyInfo = parent::getCacheKeyInfo();

        $cacheKeyInfo['store_id'] = $this->getStoreId(); // Add store ID to the cache key

        $cacheKeyInfo['custom_data'] = $this->getCustomData(); // Add custom data to the cache key

        return $cacheKeyInfo;

    }



    /**

     * Get cache tags

     *

     * @return array

     */

    public function getCacheTags()

    {

        $tags = parent::getCacheTags();

        $tags[] = 'custom_block_tag'; // Add a custom tag

        return $tags;

    }



    /**

     * Example method to get store ID

     *

     * @return int

     */

    private function getStoreId()

    {

        return $this->_storeManager->getStore()->getId();

    }



    /**

     * Example method to get custom data

     *

     * @return string

     */

    private function getCustomData()

    {

        return 'some_dynamic_value'; // Replace with your logic

    }

}

 

In this example:

  • The block is cached for 24 hours.
  • The cache key includes the store ID and some custom data to ensure uniqueness.

A custom cache tag is added for invalidation.

 

If the issue will be resolved, Click Kudos & Accept as a Solution.