Skip to content

Commit bfa4d40

Browse files
committed
AC-13719: Cache Keys associated with FPC on Magento 2.4.7 multi-store implementations
Plugin for IdentifierForSave class which adds the MAGE_RUN_TYPE and MAGE_RUN_CODE prefix to the cache key.
1 parent 4d251ae commit bfa4d40

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All rights reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageCache\Model\App;
9+
10+
use Magento\Store\Model\StoreManager;
11+
12+
/**
13+
* Class CacheIdentifierForSavePlugin
14+
*
15+
* Should add design exceptions to identifier for built-in cache when saving
16+
*/
17+
class CacheIdentifierForSavePlugin
18+
{
19+
/**
20+
* @var \Magento\Framework\View\DesignExceptions
21+
*/
22+
private $designExceptions;
23+
24+
/**
25+
* @var \Magento\Framework\App\RequestInterface
26+
*/
27+
private $request;
28+
29+
/**
30+
* @var \Magento\PageCache\Model\Config
31+
*/
32+
private $config;
33+
34+
/**
35+
* @param \Magento\Framework\View\DesignExceptions $designExceptions
36+
* @param \Magento\Framework\App\RequestInterface $request
37+
* @param \Magento\PageCache\Model\Config $config
38+
*/
39+
public function __construct(
40+
\Magento\Framework\View\DesignExceptions $designExceptions,
41+
\Magento\Framework\App\RequestInterface $request,
42+
\Magento\PageCache\Model\Config $config
43+
) {
44+
$this->designExceptions = $designExceptions;
45+
$this->request = $request;
46+
$this->config = $config;
47+
}
48+
49+
/**
50+
* Adds a theme key to identifier for a built-in cache if user-agent theme rule is actual
51+
*
52+
* @param \Magento\PageCache\Model\App\Request\Http\IdentifierForSave $identifier
53+
* @param string $result
54+
* @return string
55+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
56+
*/
57+
public function afterGetValue(\Magento\PageCache\Model\App\Request\Http\IdentifierForSave $identifier, $result)
58+
{
59+
if ($this->config->getType() === \Magento\PageCache\Model\Config::BUILT_IN && $this->config->isEnabled()) {
60+
$identifierPrefix = '';
61+
62+
$ruleDesignException = $this->designExceptions->getThemeByRequest($this->request);
63+
if ($ruleDesignException !== false) {
64+
$identifierPrefix .= 'DESIGN' . '=' . $ruleDesignException . '|';
65+
}
66+
67+
if ($runType = $this->request->getServerValue(StoreManager::PARAM_RUN_TYPE)) {
68+
$identifierPrefix .= StoreManager::PARAM_RUN_TYPE . '=' . $runType . '|';
69+
}
70+
71+
if ($runCode = $this->request->getServerValue(StoreManager::PARAM_RUN_CODE)) {
72+
$identifierPrefix .= StoreManager::PARAM_RUN_CODE . '=' . $runCode . '|';
73+
}
74+
75+
return $identifierPrefix . $result;
76+
}
77+
return $result;
78+
}
79+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
<type name="Magento\Framework\App\PageCache\Identifier">
1010
<plugin name="core-app-area-design-exception-plugin" type="Magento\PageCache\Model\App\CacheIdentifierPlugin" sortOrder="10"/>
1111
</type>
12+
<type name="Magento\PageCache\Model\App\Request\Http\IdentifierForSave">
13+
<plugin name="core-app-area-design-exception-plugin-for-save" type="Magento\PageCache\Model\App\CacheIdentifierForSavePlugin" sortOrder="10"/>
14+
</type>
1215
<type name="Magento\Framework\App\PageCache\Cache">
1316
<plugin name="fpc-type-plugin" type="Magento\PageCache\Model\App\PageCachePlugin"/>
1417
</type>

dev/tests/integration/testsuite/Magento/PageCache/Model/App/CacheIdentifierPluginTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\PageCache\Identifier;
1212
use Magento\Framework\App\Request\Http;
1313
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\PageCache\Model\App\Request\Http\IdentifierForSave;
1415
use Magento\PageCache\Model\Cache\Type;
1516
use Magento\Store\Model\StoreManager;
1617
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
@@ -44,6 +45,11 @@ class CacheIdentifierPluginTest extends TestCase
4445
*/
4546
private $identifier;
4647

48+
/**
49+
* @var IdentifierForSave
50+
*/
51+
private $identifierForSave;
52+
4753
/**
4854
* @var DataFixtureStorage
4955
*/
@@ -64,6 +70,7 @@ protected function setUp(): void
6470
$this->objectManager = Bootstrap::getObjectManager();
6571
$this->request = $this->objectManager->get(Http::class);
6672
$this->identifier = $this->objectManager->get(Identifier::class);
73+
$this->identifierForSave = $this->objectManager->get(IdentifierForSave::class);
6774
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
6875
$this->cacheState = $this->objectManager->get(StateInterface::class);
6976

@@ -106,5 +113,10 @@ public function testAfterGetValueWithRunTypeAndCode()
106113
$this->assertNotEmpty($result);
107114
$this->assertStringContainsString('MAGE_RUN_TYPE=store', $result);
108115
$this->assertStringContainsString('MAGE_RUN_CODE=' . $storeCode, $result);
116+
117+
$resultForSave = $this->identifierForSave->getValue();
118+
$this->assertNotEmpty($resultForSave);
119+
$this->assertStringContainsString('MAGE_RUN_TYPE=store', $resultForSave);
120+
$this->assertStringContainsString('MAGE_RUN_CODE=' . $storeCode, $resultForSave);
109121
}
110122
}

0 commit comments

Comments
 (0)