Blog

How to disable the cache for the block in Magento 2?

Introduction:

Magento 2 caching is helpful in the page loading speed of the website. As we all know Magento websites take much time to load and in solving that the caching was introduced. Many times it is not necessary to always go for caching configurations but one can disable cache for some specific blocks only.

Today we discuss about Magento 2 disable cache block. There are 3 ways to do it.

  1. Disable cache from layout xml for block or
  2. Disable cache for block programatically
  3. Using Varnish Cache
  4. Clear Cache Programmatically

So let start with our example.

Method 1: Disable cache from layout xml for block

Blocks can be set as non-cacheable by setting the cacheable attribute false in layout XML files.

For example

<block class="Block\Class" name="blockname" cacheable="false" />

Pages containing such blocks are not cached.

Just add this cacheable=”false” into the XML file like:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <body>
      <referenceContainer name="content">
         <block class="Vendor\Module\Block\Classname" name="blockname" template="Vendor_Module::filename.phtml" cacheable="false"/>
      </referenceContainer>
    </body>
</page>

You can use cacheable=”false” attribute in your layout to disable cache for block.

Note:- Pages containing such blocks are not cached.

Method 2: Disable cache for block programatically

Now the second option is disable cache for block programmatically.

<?php
namespace Vendor\Module\Block;
class Classname extends \Magento\Framework\View\Element\Template {
    public function getCacheLifetime()
    {
        return null;
    }
}

Method 3: Using Varnish Cache

There is provision in the FPC to set a different TTL for a block should if need it, and this is done in the layout XML by setting a ttl attribute to the chosen value in seconds. When rendering the block the FPC detects the TTL value and instead of displaying the block as normal, it instead wraps it in an Edge Side Include (ESI) tag. The caching application (e.g. Varnish) then subsequently requests the block in a separate request.

When using Varnish you can use the ttl attribute instead, like:

<block class="Block\Class" name="blockname" ttl="0" />

Here you can see that a ttl attribute has been set on the block with a value of 0 seconds.

Method 4: Clear Cache Programmatically

Fourth method is to programmatically clear cache. Click the below link for the details.

How to clear cache programmatically in Magento 2

Note:

Do not use the $_isScopePrivate property in your blocks. This property is obsolete and will not work properly.

Conclusion:

This is how easy to disable custom block caching in Magento 2. We hope that you found this tutorial helpful. If you have any questions or queries, please ask them in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

🚀 Let’s Connect on LinkedIn! 🚀

Want to level up your skills and knowledge? 🚀
Follow us on LinkedIn to get:
✅ Expert insights on business growth
✅ Daily tips to sharpen your skills
✅ Exclusive updates from The Coach SMB

Let's grow together!