Magento 2 Block Cache

Caching plays an essential role in improving the page load time. In this article, I want to show you five blocks cache in Magento 2, or five approaches to caching, and when and how to use them. Interestingly, this article will cover both full page and block cache. Let’s explore each type of block cache together!

In case, cache_lifetime of the block is set to a number greater than 0, the block is cacheable

 

to cache any block, you need to override this function 

 

protected function getCacheLifetime()
{
    return 3600;
}
//below method to keep cache key unique for each block or page (try to set it as you like).
public function getCacheKeyInfo()
{
    $name =   $this->getProduct()->getId();
    $keyInfo     =  parent::getCacheKeyInfo();
    $keyInfo[]   =  $name; // adding the product id to the cache key so that each cache built remains unique as per the product page.
    return $keyInfo;
}