Magento – Magento 2 clear particular block cache

block-cachecachemagento2

I have a block that displays a product grid with SKUs mention in admin system configuration and I have provided cache lifetime for that block as 3600 sec. It works perfectly fine.

Now when admin makes the changes in SKUs list in admin configuration it does not reflect the updated version immediately. How can I clear that particular block cache after admin saves the SKUs list?

Best Answer

Finally, I got a solution:

In your custom block, provide a cache tag for your block by assigning cache_tags a unique value for your block.

e.g. in my case, I will set the tag as cache_tags[] = 'custom_skus'

Or you can implement IdentityInterface in your custom block and then override getIdentities method, which will return array of tags.

e.g.

public function getIdentities()
{
    return ['custom_skus_block'];
}

Now, you can observe the save after of admin config and clean the tags using $this->_cacheManager->clean($tags);. Refer here for event of admin config save.

Hope this helps!

Related Topic