Site icon MageComp Blog

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:

How to use Cacheable Block:

<block class="Your_class_name_block">

    <arguments>

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

    </arguments>

</block>
<type name="your_block_here">

    <arguments>

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

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

        </argument>

    </arguments>

</type>
$block->setCacheLifetime(7200);

// or

$block->setData('cache_lifetime');
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:

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:

How to use Page Cache Killer  Block:

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

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:

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!

Exit mobile version