Magento 2 FPC and clean cache by tag

Magento 2 has buit in FPC or full page cache. In some cases we want to clean or remove some cache. It can be done over tag or cache ids.

Our first example will be demonstrate how to clean with tag.

Firstly, we need to get cache instance, you can get it with next method:

This method you can implement is your php class.

Now, cleaning logic by tag is demonstrated below:

Our code in php method cleanByTags will be delete cache which is related to cache tag ‘CAT_C_38

Magento 2 uses cache logic form Zend Framework you can check documetation for more info on url https://framework.zend.com/manual/1.10/en/zend.cache.theory.html

 

Also every php model in Magento 2 dispatches event ‘clean_cache_by_tags’ in method afterSave

There is observer class Magento\PageCache\Observer\FlushcacheBytags, in this class is implemented cleaning logic for all models. Model has to have method getIdentities.

This method returns array of tags. For more info check this method in class Magent\Catalog\Model\Product and it will be more clear. On every product save event is dispatched by afterSave method and cache is invalidated for specific tags.

You may also like...

Leave a Reply