Magento 2 Block Cache

To improve the page load time in Magento 2, caching plays an important role. There are five types of cache in Magento2 This article will cover both full page and block cache. Let’s explore each type of block cache together.

  1. Cacheable block
  2. Non-Cacheable block
  3. Page Cache Killer
  4. Private block
  5. ESI block

Cacheable Block

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

When To Use:

We should use the cacheable block in the following situations:

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

How To Use:

You need to take the following actions to use the cacheable block:

Setting cache_lifetime via layouts XML:

<block class="MyBlock">
    <arguments>
        <argument name="cache_lifetime" xsi:type="number">3600</argument>
    </arguments>
</block>

Setting cache_lifetime via di.xml 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', 3600);

Overriding retriever method:

class MyBlock extends AbstractBlock
{
    protected function getCacheLifetime()
    {
        return 3600;
    }
}

Non-cacheable Block

In this 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:

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

Page Cache Killer

In this 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 condition:

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

How To Use:

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

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

Private Block

In this case, the _isScopePrivate property of block class is set to true, then the block is private block. 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 this case, if the declaration of the block in the XML layout has ttl attribute, then the block is ESI. Only when full page cache application is set to Varnish, ESI blocks are actual. ESI block is fetched by a separate Varnish request which is cached and invalidated independently from the page.

When To Use:

You should use the ESI block in the following situation:

  • If the block is considered to be invalidated more frequently than the pages where this block is rendered.
  • If the block is considered to be invalidated less frequently than the 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"/>

Leave a Reply