Skip to content

Commit 69e0962

Browse files
committed
ACP2E-3063: [Cloud] Cache is not getting invalidated.
- with test
1 parent 8acc379 commit 69e0962

File tree

8 files changed

+52
-95
lines changed

8 files changed

+52
-95
lines changed

app/code/Magento/Catalog/Model/Category/CategoryLayoutTagCacheResolver.php renamed to app/code/Magento/Catalog/Model/Category/LayoutCacheTagResolver.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* Get additional layout cache tag for category layout.
2828
*/
29-
class CategoryLayoutTagCacheResolver implements StrategyInterface
29+
class LayoutCacheTagResolver implements StrategyInterface
3030
{
3131
/**
3232
* @inheritDoc
@@ -40,7 +40,6 @@ public function getTags($object)
4040
str_replace('{{ID}}', $object->getId(), Instance::SINGLE_CATEGORY_LAYOUT_HANDLE))
4141
];
4242
}
43-
4443
return [];
4544
}
4645

@@ -52,8 +51,8 @@ public function getTags($object)
5251
*/
5352
private function isExistingCategoryLayoutChange(AbstractModel $object): bool
5453
{
55-
return !$object instanceof Category || $this->isObjectChanged($object) ||
56-
$object->isObjectNew();
54+
return $object instanceof Category &&
55+
!$object->isObjectNew() && $this->isObjectChanged($object);
5756
}
5857

5958
/**
@@ -65,15 +64,10 @@ private function isExistingCategoryLayoutChange(AbstractModel $object): bool
6564
private function isObjectChanged(AbstractModel $object): bool
6665
{
6766
$isChanged = false;
68-
if ($object->isDeleted() || $object->hasDataChanges()) {
67+
$objectNewPageLayout = $object->getData('page_layout');
68+
$objectOldPageLayout = $object->getOrigData('page_layout');
69+
if ($objectNewPageLayout !== $objectOldPageLayout) {
6970
$isChanged = true;
70-
} else {
71-
$objectNewPageLayout = $object->getData('page_layout');
72-
$objectOldPageLayout = $object->getOrigData('page_layout');
73-
74-
if ($objectNewPageLayout !== $objectOldPageLayout) {
75-
$isChanged = true;
76-
}
7771
}
7872
return $isChanged;
7973
}

app/code/Magento/Catalog/Model/Product/ProductLayoutTagCacheResolver.php renamed to app/code/Magento/Catalog/Model/Product/LayoutCacheTagResolver.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/**
2727
* Get additional layout cache tag for product layout.
2828
*/
29-
class ProductLayoutTagCacheResolver implements StrategyInterface
29+
class LayoutCacheTagResolver implements StrategyInterface
3030
{
3131
/**
3232
* @inheritDoc
@@ -40,7 +40,6 @@ public function getTags($object)
4040
str_replace('{{ID}}', $object->getId(), Instance::SINGLE_PRODUCT_LAYOUT_HANDLE))
4141
];
4242
}
43-
4443
return [];
4544
}
4645

@@ -52,8 +51,8 @@ public function getTags($object)
5251
*/
5352
private function isExistingProductLayoutChange(AbstractModel $object): bool
5453
{
55-
return !$object instanceof Product || $this->isObjectChanged($object) ||
56-
$object->isObjectNew();
54+
return $object instanceof Product &&
55+
!$object->isObjectNew() && $this->isObjectChanged($object);
5756
}
5857

5958
/**
@@ -65,15 +64,10 @@ private function isExistingProductLayoutChange(AbstractModel $object): bool
6564
private function isObjectChanged(AbstractModel $object): bool
6665
{
6766
$isChanged = false;
68-
if ($object->isDeleted() || $object->hasDataChanges()) {
67+
$objectNewPageLayout = $object->getData('page_layout');
68+
$objectOldPageLayout = $object->getOrigData('page_layout');
69+
if ($objectNewPageLayout !== $objectOldPageLayout) {
6970
$isChanged = true;
70-
} else {
71-
$objectNewPageLayout = $object->getData('page_layout');
72-
$objectOldPageLayout = $object->getOrigData('page_layout');
73-
74-
if ($objectNewPageLayout !== $objectOldPageLayout) {
75-
$isChanged = true;
76-
}
7771
}
7872
return $isChanged;
7973
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,14 +1359,14 @@
13591359
</argument>
13601360
</arguments>
13611361
</type>
1362-
<type name="Magento\Theme\Model\LayoutTagCacheFactory">
1362+
<type name="Magento\Theme\Model\LayoutCacheTagResolverFactory">
13631363
<arguments>
1364-
<argument name="customLayoutTagCache" xsi:type="array">
1364+
<argument name="cacheTagsResolvers" xsi:type="array">
13651365
<item name="Magento\Catalog\Model\Product" xsi:type="object">
1366-
Magento\Catalog\Model\Product\ProductLayoutTagCacheResolver
1366+
Magento\Catalog\Model\Product\LayoutCacheTagResolver
13671367
</item>
13681368
<item name="Magento\Catalog\Model\Category" xsi:type="object">
1369-
Magento\Catalog\Model\Category\CategoryLayoutTagCacheResolver
1369+
Magento\Catalog\Model\Category\LayoutCacheTagResolver
13701370
</item>
13711371
</argument>
13721372
</arguments>

app/code/Magento/Cms/Model/Page/CmsLayoutTagCacheResolver.php renamed to app/code/Magento/Cms/Model/Page/LayoutCacheTagResolver.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818

1919
namespace Magento\Cms\Model\Page;
2020

21+
use Magento\Cms\Model\Page;
2122
use Magento\Framework\App\Cache\Tag\StrategyInterface;
2223
use Magento\Framework\Model\AbstractModel;
2324

2425
/**
2526
* Get additional layout cache tag for CMS layout.
2627
*/
27-
class CmsLayoutTagCacheResolver implements StrategyInterface
28+
class LayoutCacheTagResolver implements StrategyInterface
2829
{
2930
/**
3031
* @inheritDoc
@@ -39,7 +40,6 @@ public function getTags($object)
3940
str_replace('-', '_', strtoupper($object->getIdentifier())))
4041
];
4142
}
42-
4343
return [];
4444
}
4545

@@ -51,8 +51,7 @@ public function getTags($object)
5151
*/
5252
private function isExistingPageLayoutChange(AbstractModel $object): bool
5353
{
54-
return !$object instanceof Page ||
55-
!$object->dataHasChangedFor(Page::PAGE_LAYOUT) ||
56-
$object->isObjectNew();
54+
return $object instanceof Page && !$object->isObjectNew() &&
55+
$object->dataHasChangedFor(Page::PAGE_LAYOUT);
5756
}
5857
}

app/code/Magento/Cms/etc/di.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,11 @@
316316
</argument>
317317
</arguments>
318318
</virtualType>
319-
<type name="Magento\Theme\Model\LayoutTagCacheFactory">
319+
<type name="Magento\Theme\Model\LayoutCacheTagResolverFactory">
320320
<arguments>
321-
<argument name="customLayoutTagCache" xsi:type="array">
321+
<argument name="cacheTagsResolvers" xsi:type="array">
322322
<item name="Magento\Cms\Model\Page" xsi:type="object">
323-
Magento\Cms\Model\Page\CmsLayoutTagCacheResolver
323+
Magento\Cms\Model\Page\LayoutCacheTagResolver
324324
</item>
325325
</argument>
326326
</arguments>

app/code/Magento/Theme/Model/LayoutTagCacheFactory.php renamed to app/code/Magento/Theme/Model/LayoutCacheTagResolverFactory.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,22 @@
2424
/**
2525
* Creates strategies for layout cache
2626
*/
27-
class LayoutTagCacheFactory
27+
class LayoutCacheTagResolverFactory
2828
{
2929
/**
30-
* @var array
31-
*/
32-
private array $customLayoutTagCache= [];
33-
34-
/**
35-
* Factory constructor.
36-
*
37-
* @param array $customLayoutTagCache
30+
* Construct
3831
*/
3932
public function __construct(
40-
array $customLayoutTagCache = []
41-
) {
42-
$this->customLayoutTagCache = $customLayoutTagCache ;
43-
}
33+
private readonly array $cacheTagsResolvers
34+
) {}
4435

4536
/**
46-
* Return tag strategy for specified object
37+
* Return tag resolver for specified object
4738
*
4839
* @param object $object
4940
* @return StrategyInterface|null
5041
*/
51-
public function getStrategy(object $object): ?StrategyInterface
42+
public function getResolver(object $object): ?StrategyInterface
5243
{
5344
if (!is_object($object)) {
5445
throw new InvalidArgumentException('Provided argument is not an object');
@@ -60,9 +51,9 @@ class_parents($object),
6051
class_implements($object)
6152
);
6253

63-
$result = array_intersect(array_keys($this->customLayoutTagCache), $classHierarchy);
54+
$result = array_intersect(array_keys($this->cacheTagsResolvers), $classHierarchy);
6455
if ($result) {
65-
return $this->customLayoutTagCache [array_shift($result)];
56+
return $this->cacheTagsResolvers[array_shift($result)];
6657
}
6758
return null;
6859
}

app/code/Magento/Theme/Observer/InvalidateLayoutCacheObserver.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020

2121
use Magento\Framework\App\Cache\Type\Layout as LayoutCache;
2222
use Magento\Framework\App\Cache\StateInterface as CacheState;
23-
use Magento\Framework\App\Cache\Tag\Resolver;
2423
use Magento\Framework\Event\Observer;
2524
use Magento\Framework\Event\ObserverInterface;
26-
use Magento\Theme\Model\LayoutTagCacheFactory;
25+
use Magento\Theme\Model\LayoutCacheTagResolverFactory;
2726

2827
/**
2928
* Invalidates layout cache.
@@ -41,31 +40,23 @@ class InvalidateLayoutCacheObserver implements ObserverInterface
4140
private $cacheState;
4241

4342
/**
44-
* @var Resolver
43+
* @var LayoutCacheTagResolverFactory
4544
*/
46-
private $tagResolver;
47-
48-
/**
49-
* @var LayoutTagCacheFactory
50-
*/
51-
private $layoutTagCacheFactory;
45+
private $layoutCacheTagResolver;
5246

5347
/**
5448
* @param LayoutCache $layoutCache
5549
* @param CacheState $cacheState
56-
* @param Resolver $tagResolver
57-
* @param LayoutTagCacheFactory $layoutTagCacheFactory
50+
* @param LayoutCacheTagResolverFactory $layoutCacheTagResolver
5851
*/
5952
public function __construct(
6053
LayoutCache $layoutCache,
6154
CacheState $cacheState,
62-
Resolver $tagResolver,
63-
LayoutTagCacheFactory $layoutTagCacheFactory
55+
LayoutCacheTagResolverFactory $layoutCacheTagResolver
6456
) {
6557
$this->layoutCache = $layoutCache;
6658
$this->cacheState = $cacheState;
67-
$this->tagResolver = $tagResolver;
68-
$this->layoutTagCacheFactory = $layoutTagCacheFactory;
59+
$this->layoutCacheTagResolver = $layoutCacheTagResolver;
6960
}
7061

7162
/**
@@ -78,21 +69,20 @@ public function __construct(
7869
public function execute(Observer $observer): void
7970
{
8071
$object = $observer->getEvent()->getObject();
81-
$layoutTagCacheTagStrategy = $this->layoutTagCacheFactory->getStrategy($object);
72+
$tagResolver = $this->layoutCacheTagResolver->getResolver($object);
8273

83-
if (!$layoutTagCacheTagStrategy || !is_object($object)) {
74+
if (!$tagResolver || !is_object($object)) {
8475
return;
8576
}
8677

8778
if (!$this->cacheState->isEnabled(LayoutCache::TYPE_IDENTIFIER)) {
8879
return;
8980
}
9081

91-
$tags = $this->tagResolver->getTags($object);
82+
$tags = $tagResolver->getTags($object);
9283

9384
if (!empty($tags)) {
94-
$additionalTags = $layoutTagCacheTagStrategy->getTags($object);
95-
$this->layoutCache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array_merge($tags, $additionalTags));
85+
$this->layoutCache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags);
9686
}
9787
}
9888
}

app/code/Magento/Theme/Test/Unit/Observer/InvalidateLayoutCacheObserverTest.php

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818

1919
namespace Magento\Theme\Test\Unit\Observer;
2020

21-
use Magento\Framework\App\Cache\Tag\StrategyInterface;
22-
use Magento\Theme\Model\LayoutTagCacheFactory;
21+
use Magento\Theme\Model\LayoutCacheTagResolverFactory;
2322
use Magento\Theme\Observer\InvalidateLayoutCacheObserver;
2423
use Magento\Framework\App\Cache\StateInterface as CacheState;
25-
use Magento\Framework\App\Cache\Tag\Resolver;
2624
use Magento\Framework\App\Cache\Type\Layout as LayoutCache;
2725
use Magento\Framework\DataObject;
2826
use Magento\Framework\Event;
@@ -49,15 +47,10 @@ class InvalidateLayoutCacheObserverTest extends TestCase
4947
private $cacheStateMock;
5048

5149
/**
52-
* @var Resolver|MockObject
50+
* @var LayoutCacheTagResolverFactory|MockObject
5351
*/
5452
private $tagResolverMock;
5553

56-
/**
57-
* @var LayoutTagCacheFactory|MockObject
58-
*/
59-
private $layoutTagCacheFactory;
60-
6154
/**
6255
* @var Observer|MockObject
6356
*/
@@ -84,15 +77,12 @@ protected function setUp(): void
8477
->onlyMethods(['clean'])
8578
->disableOriginalConstructor()
8679
->getMockForAbstractClass();
87-
$this->layoutTagCacheFactory = $this
88-
->getMockBuilder(LayoutTagCacheFactory::class)
89-
->onlyMethods(['getStrategy'])
90-
->disableOriginalConstructor()
91-
->getMockForAbstractClass();
9280
$this->tagResolverMock = $this
93-
->getMockBuilder(Resolver::class)
81+
->getMockBuilder(LayoutCacheTagResolverFactory::class)
82+
->addMethods(['getTags'])
83+
->onlyMethods(['getResolver'])
9484
->disableOriginalConstructor()
95-
->getMock();
85+
->getMockForAbstractClass();
9686
$this->observerMock = $this
9787
->getMockBuilder(Observer::class)
9888
->disableOriginalConstructor()
@@ -116,8 +106,7 @@ protected function setUp(): void
116106
$this->invalidateLayoutCacheObserver = new InvalidateLayoutCacheObserver(
117107
$this->layoutCacheMock,
118108
$this->cacheStateMock,
119-
$this->tagResolverMock,
120-
$this->layoutTagCacheFactory
109+
$this->tagResolverMock
121110
);
122111
}
123112

@@ -163,6 +152,10 @@ public function testExecute(
163152
->expects($this->any())
164153
->method('getIdentifier')
165154
->willReturn(random_int(1, 100));
155+
$this->tagResolverMock
156+
->expects($this->any())
157+
->method('getResolver')
158+
->willReturn($cacheStrategy);
166159
$this->tagResolverMock
167160
->expects($this->any())
168161
->method('getTags')
@@ -172,10 +165,6 @@ public function testExecute(
172165
->expects($this->any())
173166
->method('clean')
174167
->willReturnSelf();
175-
$this->layoutTagCacheFactory
176-
->expects($this->any())
177-
->method('getStrategy')
178-
->willReturn($cacheStrategy);
179168
$this->invalidateLayoutCacheObserver->execute($this->observerMock);
180169
}
181170

0 commit comments

Comments
 (0)