Magento Tutorials

How to Use Magento 2 Block Cache

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 :
<block class="Your_class_name_block">

    <arguments>

        <argument name="cache_lifetime" xsi:type="number">7200</argument>

    </arguments>

</block>
  • Setting cache_lifetime via di.xml Configuration :
<type name="your_block_here">

    <arguments>

        <argument name="data" xsi:type="array">

            <item name="cache_lifetime" xsi:type="number">7200</item>

        </argument>

    </arguments>

</type>
  • Setting cache_lifetime in imperative way:
$block->setCacheLifetime(7200);

// or

$block->setData('cache_lifetime');
  • Using Custom Block (common in core modules for some reason)
class MyBlock extends AbstractBlock

{

    protected function getCacheLifetime()

    {

        return 7200;

    }

}

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 :
<block class="your_name_here" cacheable="false"/>

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

public function __construct(Context $context) {

    parent::__construct($context);

    $this->_isScopePrivate = true;

}

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:

<block class="your_name_here" ttl="7200"/>

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]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago