1

Hi I have a Observer written on save before event on catalog my events.xml is

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="mynamespace_mymodule_observer" instance="MyNameSpace\Mymodule\Model\Observer"/>
    </event>
</config>

What i want is to invalidate this product page cache if Data is changed

 public function execute(\Magento\Framework\Event\Observer $observer ){}
4

You'll be surprised, but magento 2 performs "cache invalidation" after product save. Let's take a look in module-page-cache/etc/events.xml where we find controller_action_postdispatch_catalog_product_save as listened event.

   <event name="controller_action_postdispatch_catalog_product_save">
    <observer name="flush_all_pagecache" instance="Magento\PageCache\Observer\FlushCacheByTags" />
   </event>

Next, in class Magento\PageCache\Observer\FlushCacheByTags, built-In cache is cleaned considering the tags of incoming object, in method execute()

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if ($this->_config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->_config->isEnabled()) {
            $object = $observer->getEvent()->getObject();
            if (!is_object($object)) {
                return;
            }
            $tags = $this->getTagResolver()->getTags($object);

            if (!empty($tags)) {
                $this->getCache()->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_unique($tags));
            }
        }
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.