From 510a0d364a406e481664acc2dd4c6129dc4f64c5 Mon Sep 17 00:00:00 2001 From: ivanviduka Date: Fri, 23 Aug 2024 09:55:32 +0200 Subject: [PATCH] Additional check which prevents default category and product cache tag purge if they are disabled - still works for specific tags --- Observer/InvalidateVarnishObserver.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Observer/InvalidateVarnishObserver.php b/Observer/InvalidateVarnishObserver.php index 927d5de8..74c60b0f 100644 --- a/Observer/InvalidateVarnishObserver.php +++ b/Observer/InvalidateVarnishObserver.php @@ -81,6 +81,11 @@ public function execute(\Magento\Framework\Event\Observer $observer): void continue; } $tag = $this->cacheTags->convertCacheTags($tag); + + if (!$this->isTagAllowed($tag)) { + continue; + } + if (!in_array($tag, $this->alreadyPurged)) { $tags[] = $tag; $this->alreadyPurged[] = $tag; @@ -113,4 +118,24 @@ private function canPurgeObject(\Magento\Framework\DataObject\IdentityInterface } return true; } + + /** + * Additional validation since IdentityInterface can pass canPurgeObject check (e.g. ProductRuleIndexer), but still + * hold tags of products or categories + * + * @param string $tag + * @return bool + */ + private function isTagAllowed(string $tag) + { + if ($tag === \Magento\Catalog\Model\Category::CACHE_TAG && !$this->config->canPurgeCatalogCategory()) { + return false; + } + + if ($tag === \Magento\Catalog\Model\Product::CACHE_TAG && !$this->config->canPurgeCatalogProduct()) { + return false; + } + + return true; + } }