How to Use Block Cache in Magento 2?

The page loading speed plays a vital role in the performance and SEO ranking of an online store. Sometimes, the product list is too big that causes Magento 2 stores a long-waited time to load. This negatively affects customer experience in general.

This is where Caching plays an essential role in improving page load time. In this article, I want to show you five blocks cache in Magento 2, or five approaches to caching, and when and how to use them. Interestingly, this article will cover both full page and block cache. Let’s explore each type of block cache together!

Five block types from caching point of view in Magento 2:

Cacheable Block

In case, cache_lifetime of the block is set to a number greater than 0, the block is cacheable (for block cache).

When To Use

You should use the cacheable block in the following situations:

  • The block is frequently used. For example, the same block with the same content is shown on several pages.
  • The block is a cacheable part of the non-cacheable page.

    How To Use

    Take the following actions to use the cacheable block:

  • Setting cache_lifetime via layout XML:
<block class="MyBlock">
    <arguments>
        <argument name="cache_lifetime" xsi:type="number">3600</argument>
    </arguments>
</block>
  • Setting cache_lifetime via DI configuration:
<type name="MyBlock">
    <arguments>
        <argument name="data" xsi:type="array">
            <item name="cache_lifetime" xsi:type="number">3600</item>
        </argument>
    </arguments>
</type>
  • Setting cache_lifetime wondrously in imperative way:
$block->setCacheLifetime(3600);
// or
$block->setData('cache_lifetime');
  • Overriding retriever method (often used in core modules for some reasons):
class MyBlock extends AbstractBlock
{
    protected function getCacheLifetime()
    {
        return 3600;
    }
}

Non-cacheable Block

In case, cache_lifetime of the block is NOT set to a number greater than 0, the block is non-cacheable (for block cache).

Note: By default, all blocks are non-cacheable.

When To Use

You should use the non-cacheable block in the following situations:

  • The block comes with dynamic content and is improbable to be rendered with equal content for several times.
  • The block is used only as a child of a cacheable block in layout hierarchy.

How To Use

Remember to not apply anything from the How to use section of the cacheable block.

Page Cache Killer

In case, the cacheable attribute of a block is set to false in the layout declaration, the block is a page cache killer. If any page cache killer block is displayed on a page, the entire page is not cacheable for Full Page Cache.

When To Use

You should use the page cache killer block in the following situations:

  • The block is rendering private content, but it is not private (view the next type of block)
  • The block is rendering content which should be displayed only once.
  • The block is rendering content which is regularly updated.
  • Business logic implementation includes calls to functions like rank() or mt_rand().

How To Use

To use the Page Cache Killer block, you need to set cacheable attribute of block node in the XML layout to false:

<block class="MyBlock" cacheable="false"/>

Private Block

In case, the _isScopePrivate property of block class is set to true, the block is private. Private blocks are rendered in two stages. There is only a placeholder for the private block in the main response. Separate AJAX request is retrieving actual content and puts it instead of the placeholder.

When To Use

The private block is used when:

  • The block is rendering private (session related) information on a cacheable page.

How To Use

To use the private block, you need to set the protected property of AbstractBlock _isScopePrivate to true

$this->_isScopePrivate = true;

ESI Block

In case, the declaration of the block in the XML layout has ttl attribute, the block is ESI. Only when full page cache application is set to Varnish, ESI blocks are actual. ESI block is fetched by separate Varnish request, cached and invalidated independently from the page.

When To Use

You should use the ESI block in the following situations:

  • The block is considered to be invalidated more frequently than pages where this block is rendered.
  • The block is considered to be invalidated less frequently than pages where this block is rendered.

How To Use

To use the ESI block, you need to insert TTL attribute into block declaration in layout.

<block class="MyBlock" ttl="3600"/>

Conclusion

Above are five block types in the context of caching in Magento 2. I hope that helps you understand five types of blocks cache in Magento 2 thoroughly. If you have any questions, or want to add other types, feel free to leave a comment below.

Image Description
Hello, I'm the Chief Technology Officer of Mageplaza, and I am thrilled to share my story with you. My deep love and passion for technology have fueled my journey as a professional coder and an ultra-marathon runner. Over the past decade, I have accumulated extensive experience and honed my expertise in PHP development.
x