Skip to content

Commit a9b5207

Browse files
committed
ACP2E-3063: [Cloud] Cache is not getting invalidated.
- with test
1 parent 1e317e4 commit a9b5207

File tree

6 files changed

+97
-44
lines changed

6 files changed

+97
-44
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,9 +1359,9 @@
13591359
</argument>
13601360
</arguments>
13611361
</type>
1362-
<virtualType name="Magento\Theme\Model\LayoutCacheTagResolverFactory" type="Magento\Framework\App\Cache\Tag\Strategy\Factory">
1362+
<type name="Magento\Theme\Model\LayoutCacheTagResolverFactory">
13631363
<arguments>
1364-
<argument name="customStrategies" xsi:type="array">
1364+
<argument name="cacheTagsResolvers" xsi:type="array">
13651365
<item name="Magento\Catalog\Model\Product" xsi:type="object">
13661366
Magento\Catalog\Model\Product\LayoutCacheTagResolver
13671367
</item>
@@ -1370,5 +1370,5 @@
13701370
</item>
13711371
</argument>
13721372
</arguments>
1373-
</virtualType>
1373+
</type>
13741374
</config>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,13 @@
316316
</argument>
317317
</arguments>
318318
</virtualType>
319-
<virtualType name="Magento\Theme\Model\LayoutCacheTagResolverFactory" type="Magento\Framework\App\Cache\Tag\Strategy\Factory">
319+
<type name="Magento\Theme\Model\LayoutCacheTagResolverFactory">
320320
<arguments>
321-
<argument name="customStrategies" xsi:type="array">
321+
<argument name="cacheTagsResolvers" xsi:type="array">
322322
<item name="Magento\Cms\Model\Page" xsi:type="object">
323323
Magento\Cms\Model\Page\LayoutCacheTagResolver
324324
</item>
325325
</argument>
326326
</arguments>
327-
</virtualType>
327+
</type>
328328
</config>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Theme\Model;
20+
21+
use InvalidArgumentException;
22+
use Magento\Framework\App\Cache\Tag\StrategyInterface;
23+
24+
/**
25+
* Creates strategies for layout cache
26+
*/
27+
class LayoutCacheTagResolverFactory
28+
{
29+
/**
30+
* Construct
31+
*
32+
* @param array $cacheTagsResolvers
33+
*/
34+
public function __construct(
35+
private readonly array $cacheTagsResolvers
36+
) {
37+
}
38+
39+
/**
40+
* Return tag resolver for specified object
41+
*
42+
* @param object $object
43+
* @return StrategyInterface|null
44+
*/
45+
public function getStrategy(object $object): ?StrategyInterface
46+
{
47+
if (!is_object($object)) {
48+
throw new InvalidArgumentException('Provided argument is not an object');
49+
}
50+
51+
$classHierarchy = array_merge(
52+
[get_class($object) => get_class($object)],
53+
class_parents($object),
54+
class_implements($object)
55+
);
56+
57+
$result = array_intersect(array_keys($this->cacheTagsResolvers), $classHierarchy);
58+
if ($result) {
59+
return $this->cacheTagsResolvers[array_shift($result)];
60+
}
61+
return null;
62+
}
63+
}

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Magento\Framework\App\Cache\StateInterface as CacheState;
2323
use Magento\Framework\Event\Observer;
2424
use Magento\Framework\Event\ObserverInterface;
25-
use Magento\Framework\App\Cache\Tag\Strategy\Factory;
25+
use Magento\Theme\Model\LayoutCacheTagResolverFactory;
2626

2727
/**
2828
* Invalidates layout cache.
@@ -40,25 +40,23 @@ class InvalidateLayoutCacheObserver implements ObserverInterface
4040
private $cacheState;
4141

4242
/**
43-
* Tag strategies factory
44-
*
45-
* @var Factory
43+
* @var LayoutCacheTagResolverFactory
4644
*/
47-
private $strategyFactory;
45+
private $layoutCacheTagResolver;
4846

4947
/**
5048
* @param LayoutCache $layoutCache
5149
* @param CacheState $cacheState
52-
* @param Factory $factory
50+
* @param LayoutCacheTagResolverFactory $layoutCacheTagResolver
5351
*/
5452
public function __construct(
5553
LayoutCache $layoutCache,
5654
CacheState $cacheState,
57-
Factory $factory
55+
LayoutCacheTagResolverFactory $layoutCacheTagResolver
5856
) {
5957
$this->layoutCache = $layoutCache;
6058
$this->cacheState = $cacheState;
61-
$this->strategyFactory = $factory;
59+
$this->layoutCacheTagResolver = $layoutCacheTagResolver;
6260
}
6361

6462
/**
@@ -71,16 +69,17 @@ public function __construct(
7169
public function execute(Observer $observer): void
7270
{
7371
$object = $observer->getEvent()->getObject();
72+
$tagResolver = $this->layoutCacheTagResolver->getStrategy($object);
7473

75-
if (!is_object($object)) {
74+
if (!$tagResolver || !is_object($object)) {
7675
return;
7776
}
7877

7978
if (!$this->cacheState->isEnabled(LayoutCache::TYPE_IDENTIFIER)) {
8079
return;
8180
}
8281

83-
$tags = $this->strategyFactory->getStrategy($object)->getTags($object);
82+
$tags = $tagResolver->getTags($object);
8483

8584
if (!empty($tags)) {
8685
$this->layoutCache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags);

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

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

21-
use Magento\Framework\App\Cache\Tag\Strategy\Factory;
22-
use Magento\Framework\App\Cache\Tag\StrategyInterface;
21+
use Magento\Theme\Model\LayoutCacheTagResolverFactory;
2322
use Magento\Theme\Observer\InvalidateLayoutCacheObserver;
2423
use Magento\Framework\App\Cache\StateInterface as CacheState;
2524
use Magento\Framework\App\Cache\Type\Layout as LayoutCache;
@@ -47,9 +46,9 @@ class InvalidateLayoutCacheObserverTest extends TestCase
4746
private $cacheStateMock;
4847

4948
/**
50-
* @var Factory|MockObject
49+
* @var LayoutCacheTagResolverFactory|MockObject
5150
*/
52-
private $strategyFactory;
51+
private $tagResolverMock;
5352

5453
/**
5554
* @var Observer|MockObject
@@ -76,12 +75,13 @@ protected function setUp(): void
7675
->getMockBuilder(LayoutCache::class)
7776
->onlyMethods(['clean'])
7877
->disableOriginalConstructor()
79-
->getMock();
80-
$this->strategyFactory = $this
81-
->getMockBuilder(Factory::class)
78+
->getMockForAbstractClass();
79+
$this->tagResolverMock = $this
80+
->getMockBuilder(LayoutCacheTagResolverFactory::class)
81+
->addMethods(['getTags'])
8282
->onlyMethods(['getStrategy'])
8383
->disableOriginalConstructor()
84-
->getMock();
84+
->getMockForAbstractClass();
8585
$this->observerMock = $this
8686
->getMockBuilder(Observer::class)
8787
->disableOriginalConstructor()
@@ -105,7 +105,7 @@ protected function setUp(): void
105105
$this->invalidateLayoutCacheObserver = new InvalidateLayoutCacheObserver(
106106
$this->layoutCacheMock,
107107
$this->cacheStateMock,
108-
$this->strategyFactory
108+
$this->tagResolverMock
109109
);
110110
}
111111

@@ -115,21 +115,17 @@ protected function setUp(): void
115115
* @param bool $cacheIsEnabled
116116
* @param bool $isDataChangedFor
117117
* @param bool $isObjectNew
118+
* @param object|null $cacheStrategy
118119
* @param array $tags
119-
* @throws RandomException
120120
* @dataProvider invalidateLayoutCacheDataProvider
121121
*/
122122
public function testExecute(
123123
bool $cacheIsEnabled,
124124
bool $isDataChangedFor,
125125
bool $isObjectNew,
126+
?object $cacheStrategy,
126127
array $tags
127128
): void {
128-
$cacheStrategy = $this
129-
->getMockBuilder(StrategyInterface::class)
130-
->onlyMethods(['getTags'])
131-
->disableOriginalConstructor()
132-
->getMock();
133129
$this->observerMock
134130
->expects($this->atLeastOnce())
135131
->method('getEvent')
@@ -153,12 +149,12 @@ public function testExecute(
153149
$this->objectMock
154150
->expects($this->any())
155151
->method('getIdentifier')
156-
->willReturn(random_int(1, 100));
157-
$this->strategyFactory
152+
->willReturn(10);
153+
$this->tagResolverMock
158154
->expects($this->any())
159155
->method('getStrategy')
160156
->willReturn($cacheStrategy);
161-
$cacheStrategy
157+
$this->tagResolverMock
162158
->expects($this->any())
163159
->method('getTags')
164160
->with($this->objectMock)
@@ -178,11 +174,11 @@ public function testExecute(
178174
public static function invalidateLayoutCacheDataProvider(): array
179175
{
180176
return [
181-
'when layout cache is not enabled' => [false, true, false, []],
182-
'when cache is not changed' => [true, false, false, []],
183-
'when object is new' => [true, true, false, []],
184-
'when tag is empty' => [true, true, false, []],
185-
'when tag is not empty' => [true, true, false, ['cms_p']],
177+
'when layout cache is not enabled' => [false, true, false, null, []],
178+
'when cache is not changed' => [true, false, false, null, []],
179+
'when object is new' => [true, true, false, null, []],
180+
'when tag is empty' => [true, true, false, null, []],
181+
'when tag is not empty' => [true, true, false, null, ['cms_p']],
186182
];
187183
}
188184
}

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,4 @@
334334
<type name="Magento\Config\Console\Command\LocaleEmulator">
335335
<plugin name="themeForLocaleEmulator" type="Magento\Theme\Plugin\LocaleEmulator"/>
336336
</type>
337-
<type name="Magento\Theme\Observer\InvalidateLayoutCacheObserver">
338-
<arguments>
339-
<argument name="strategyFactory" xsi:type="object">Magento\Theme\Model\LayoutCacheTagResolverFactory</argument>
340-
</arguments>
341-
</type>
342337
</config>

0 commit comments

Comments
 (0)