Hello, Magento Friends!

Here I am with another tutorial and this time I have compiled the subject matter on How to use Magento 2 Block Cache. I have stated the 5 ways in which caching is possible in Magento 2 and also how and when to use these 5 techniques of caching.

Sometimes, when the product list is too big in Magento 2, it takes a longer time to load a page in Magento 2. Surprisingly, now you can easily reduce the loading time of the page by appending a particular block to the cache. Below, we have enlisted the different ways you can meet this objective.

Let’s get right in!

How to Use Magento 2 Block Cache

Cacheable Block

The Block is cacheable when the cache_lifetime of the block is fixed to a number greater than 0.

Use Cacheable Block when:

  • The Block is repeatedly used. When the corresponding block with the related content is presented on multiple pages. 
  • The block consists of a cacheable part in a non-cacheable page.

How to use Cacheable Block:

  • Setting cache_lifetime via layout XML :
  • Setting cache_lifetime via di.xml Configuration :
  • Setting cache_lifetime in imperative way:
  • Using Custom Block (common in core modules for some reason)

Non-Cacheable Block

The block is non-cacheable when the cache_lifetime of the block is NOT fixed to a number bigger than 0. Blocks are non-cacheable at first.

Use Non-Cacheable Block when:

  • Contains dynamic content and is implausible to be imparted with equal content multiple times.
  • The block is applied solely as a child of the cacheable block in the design hierarchy.

Page Cache Killer

The block is a page cache killer in the event that the cacheable attribute is fixed to false in the layout declaration. If at all even one such block is there on a page then the whole page is not cacheable for the full page cache.

Use Page Cache Killer Block when:

  • The block is not private yet presenting such content. 
  • The block is presenting the content that is required only once.
  • The block is presenting content updating regularly
  • The business logic implementation contains calls to functions. For instance rand() or mt_rand().

How to use Page Cache Killer  Block:

  • Setting cacheable via layout XML :

Private Block

The block is private if the _isScopePrivate property of block class is fixed to true. Rendering of a private block is in two stages. The main response comprises only a placeholder for the block. A separate AJAX request retrieves actual content and puts it in lieu of the placeholder.

Use Private Block when:

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

How to use Private Block:

In block class set _isScopePrivate property to true on __construct() function:

So add this to your class

ESI Block

If the declaration of the block consists of the ttl attribute then the block is said to be an ESI block. ESI blocks are said to be real only when the full page cache application is set to varnish. To fetch the ESI blocks, you can do so by separate varnish requests, cached and invalidated independently from the page.

Use Page Cache Killer Block when:

  • Whenever the ESI block is presented, the block is nullified more often than pages.
  • Whenever the ESI block is presented, the block is nullified less often than pages.

How to use Page Cache Killer  Block:

You need to insert TTL attribute into block declaration in layout:

Summing It Up:

So, here I conclude with How to use Magento 2 Block Cache. Perform carefully and you will get the results. In case of queries, reach out to us via the comments section below. 

Have a G’day!

Happy Reading!

Click to rate this post!
[Total: 5 Average: 5]