7

If you change related products or any other attribute from Magento2's backend, it reflects immediately on the storefront (even though FPC is enabled).

On closer inspection, it is found that it's calling the cache cleaning method on Magento\Catalog\Model\Product::beforeSave():

$this->_cacheManager->clean('catalog_product_' . $this->getId());

Is this a bug or feature in Magento2?

But I bet this is not what we expected. At least the changes done via backend should not be reflected unless the cache is refreshed.

What you say guys?

== EDIT ==

To disable the cache clear happening from backend, I simply removed the functionality from Magento\Framework\Cache\Frontend\Decorator\TagScope::clean() as:

public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = [])
{
    $result = true;
    /*if ($mode == \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG) {
        $result = false;
        foreach ($tags as $tag) {
            if (parent::clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [$tag, $this->getTag()])) {
                $result = true;
            }
        }
    } else {
        if ($mode == \Zend_Cache::CLEANING_MODE_ALL) {
            $mode = \Zend_Cache::CLEANING_MODE_MATCHING_TAG;
            $tags = [$this->getTag()];
        } else {
            $tags[] = $this->getTag();
        }
        $result = parent::clean($mode, $tags);
    }*/
    return $result;
}
1
  • Good point raised. +1 Apr 8, 2019 at 9:57

1 Answer 1

7

This is a feature!

Magento 2 out of the box will clear correct caches on the frontend depending on cache tags. Each entity that impacts the cache will have it's own tags. Once a product is saved all of the pages on which that product appears will be cleared. No need to flush the cache.

See more info here: http://devdocs.magento.com/guides/v2.1/config-guide/cache/cache-priv-inval.html

The Magento application enables you to clear cached content immediately after a entity changes. We use an IdentityInterface to link entities in the application with cached content and to know what cache to clear when an entity changes.

2
  • What will be the consequences if I remove the cache cleaning functionality from beforeSave() method?
    – MagePsycho
    Mar 13, 2017 at 19:53
  • Your cache will not be cleaned until you flush the page cache and block cache. If you really want to do this, you are better off creating a preference for the product model and rewriting \Magento\Catalog\Model\Product::cleanCache so that it does nothing.
    – nikola99
    Mar 13, 2017 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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