Skip to content

Commit 77a5b3f

Browse files
committed
ACP2E-3063: [Cloud] Cache is not getting invalidated.
- without test
1 parent 3e50ec8 commit 77a5b3f

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Cms\Model;
9+
10+
use Magento\Framework\App\Cache\Tag\StrategyInterface;
11+
12+
/**
13+
* Provides the cms layout cache identity to invalidate on layout change.
14+
*/
15+
class TagsStrategy implements StrategyInterface
16+
{
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function getTags($object)
21+
{
22+
return [
23+
sprintf('%s_%s', Page::CACHE_TAG, $object->getId()),
24+
sprintf(
25+
'%s_%s', 'CMS_PAGE_VIEW_ID',
26+
str_replace('-', '_', strtoupper($object->getIdentifier()))
27+
)
28+
];
29+
}
30+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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\Cms\Observer;
20+
21+
use Magento\Framework\App\Cache\Type\Layout as LayoutCache;
22+
use Magento\Framework\App\Cache\StateInterface as CacheState;
23+
use Magento\Framework\App\Cache\Tag\Resolver;
24+
use Magento\Framework\Event\Observer;
25+
use Magento\Framework\Event\ObserverInterface;
26+
27+
/**
28+
* Invalidates layout cache.
29+
*/
30+
class InvalidateLayoutCacheObserver implements ObserverInterface
31+
{
32+
/**
33+
* @var LayoutCache
34+
*/
35+
private $layoutCache;
36+
37+
/**
38+
* @var CacheState
39+
*/
40+
private $cacheState;
41+
42+
/**
43+
* @var Resolver
44+
*/
45+
private $tagResolver;
46+
47+
/**
48+
* @param LayoutCache $layoutCache
49+
* @param CacheState $cacheState
50+
* @param Resolver $tagResolver
51+
*/
52+
public function __construct(
53+
LayoutCache $layoutCache,
54+
CacheState $cacheState,
55+
Resolver $tagResolver
56+
) {
57+
$this->layoutCache = $layoutCache;
58+
$this->cacheState = $cacheState;
59+
$this->tagResolver = $tagResolver;
60+
}
61+
62+
/**
63+
* Clean identities of event object from layout cache
64+
*
65+
* @param Observer $observer
66+
*
67+
* @return void
68+
*/
69+
public function execute(Observer $observer)
70+
{
71+
$object = $observer->getEvent()->getObject();
72+
73+
if (!is_object($object)) {
74+
return;
75+
}
76+
77+
if (!$this->cacheState->isEnabled(LayoutCache::TYPE_IDENTIFIER)) {
78+
return;
79+
}
80+
81+
$tags = $this->tagResolver->getTags($object);
82+
83+
if (!empty($tags)) {
84+
$this->layoutCache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, $tags);
85+
}
86+
}
87+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,13 @@
316316
</argument>
317317
</arguments>
318318
</virtualType>
319+
<type name="Magento\Framework\App\Cache\Tag\Strategy\Factory">
320+
<arguments>
321+
<argument name="customStrategies" xsi:type="array">
322+
<item name="Magento\Cms\Model\Page" xsi:type="object">
323+
Magento\Cms\Model\TagsStrategy
324+
</item>
325+
</argument>
326+
</arguments>
327+
</type>
319328
</config>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@
3939
<event name="cms_page_prepare_save">
4040
<observer name="validate_cms_page" instance="Magento\Cms\Observer\PageValidatorObserver" />
4141
</event>
42+
<event name="clean_cache_by_tags">
43+
<observer name="invalidate_layout_cache" instance="Magento\Cms\Observer\InvalidateLayoutCacheObserver"/>
44+
</event>
4245
</config>

0 commit comments

Comments
 (0)