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!
Contents
The Block is cacheable when the cache_lifetime of the block is fixed to a number greater than 0.
<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; } }
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.
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.
<block class="your_name_here" cacheable="false"/>
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.
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; }
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.
You need to insert TTL attribute into block declaration in layout:
<block class="your_name_here" ttl="7200"/>
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!
In modern web development, seamless navigation and state management are crucial for delivering a smooth…
Magento Open Source 2.4.8 beta version released on October 8, 2024. The latest release of…
Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…
As the world of eCommerce continues to thrive, Shopify has become one of the most…
Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…
Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…