Skip to content

Commit c6e2b39

Browse files
Fix PageCache: async rendering of blocks can corrupt layout cache #8554 #9050 #9560
Renamed interface, LayoutCacheKeyInterface made optional in constructor, injected via di.xml, some other little fixes
1 parent bd6ed7c commit c6e2b39

File tree

7 files changed

+56
-44
lines changed

7 files changed

+56
-44
lines changed

app/code/Magento/PageCache/Controller/Block.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\Serialize\Serializer\Base64Json;
1111
use Magento\Framework\Serialize\Serializer\Json;
12+
use Magento\Framework\View\Layout\LayoutCacheKeyInterface;
1213

1314
abstract class Block extends \Magento\Framework\App\Action\Action
1415
{
@@ -30,36 +31,37 @@ abstract class Block extends \Magento\Framework\App\Action\Action
3031
/**
3132
* Layout cache keys to be able to generate different cache id for same handles
3233
*
33-
* @var \Magento\Framework\View\Layout\CacheKeyInterface
34+
* @var LayoutCacheKeyInterface
3435
*/
35-
private $cacheKey;
36+
private $layoutCacheKey;
3637

3738
/**
3839
* @var string
3940
*/
40-
private $layoutCacheKey = 'mage_pagecache';
41+
private $layoutCacheKeyName = 'mage_pagecache';
4142

4243
/**
4344
* @param \Magento\Framework\App\Action\Context $context
4445
* @param \Magento\Framework\Translate\InlineInterface $translateInline
4546
* @param Json $jsonSerializer
4647
* @param Base64Json $base64jsonSerializer
47-
* @param \Magento\Framework\View\Layout\CacheKeyInterface $cacheKey
48+
* @param LayoutCacheKeyInterface $layoutCacheKey
4849
*/
4950
public function __construct(
5051
\Magento\Framework\App\Action\Context $context,
5152
\Magento\Framework\Translate\InlineInterface $translateInline,
5253
Json $jsonSerializer = null,
5354
Base64Json $base64jsonSerializer = null,
54-
\Magento\Framework\View\Layout\CacheKeyInterface $cacheKey
55+
LayoutCacheKeyInterface $layoutCacheKey = null
5556
) {
5657
parent::__construct($context);
5758
$this->translateInline = $translateInline;
5859
$this->jsonSerializer = $jsonSerializer
5960
?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
6061
$this->base64jsonSerializer = $base64jsonSerializer
6162
?: \Magento\Framework\App\ObjectManager::getInstance()->get(Base64Json::class);
62-
$this->cacheKey = $cacheKey;
63+
$this->layoutCacheKey = $layoutCacheKey
64+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(LayoutCacheKeyInterface::class);
6365
}
6466

6567
/**
@@ -79,7 +81,7 @@ protected function _getBlocks()
7981
$handles = $this->base64jsonSerializer->unserialize($handles);
8082

8183
$layout = $this->_view->getLayout();
82-
$this->cacheKey->addCacheKey($this->layoutCacheKey);
84+
$this->layoutCacheKey->addCacheKeys($this->layoutCacheKeyName);
8385

8486
$this->_view->loadLayout($handles, true, true, false);
8587
$data = [];

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

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
</argument>
3333
</arguments>
3434
</type>
35+
<type name="Magento\PageCache\Controller\Block">
36+
<arguments>
37+
<argument name="layoutCacheKey" xsi:type="object">Magento\Framework\View\Layout\LayoutCacheKeyInterface</argument>
38+
</arguments>
39+
</type>
3540
<preference for="Magento\PageCache\Model\VclGeneratorInterface" type="Magento\PageCache\Model\Varnish\VclGenerator"/>
3641
<preference for="Magento\PageCache\Model\VclTemplateLocatorInterface" type="Magento\PageCache\Model\Varnish\VclTemplateLocator"/>
3742
</config>

app/etc/di.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<preference for="Magento\Framework\Event\ManagerInterface" type="Magento\Framework\Event\Manager\Proxy" />
6969
<preference for="Magento\Framework\View\LayoutInterface" type="Magento\Framework\View\Layout" />
7070
<preference for="Magento\Framework\View\Layout\ProcessorInterface" type="Magento\Framework\View\Model\Layout\Merge" />
71-
<preference for="Magento\Framework\View\Layout\CacheKeyInterface" type="Magento\Framework\View\Model\Layout\CacheKey" />
71+
<preference for="Magento\Framework\View\Layout\LayoutCacheKeyInterface" type="Magento\Framework\View\Model\Layout\CacheKey" />
7272
<preference for="Magento\Framework\View\Url\ConfigInterface" type="Magento\Framework\View\Url\Config" />
7373
<preference for="Magento\Framework\App\Route\ConfigInterface" type="Magento\Framework\App\Route\Config" />
7474
<preference for="Magento\Framework\App\ResourceConnection\ConfigInterface" type="Magento\Framework\App\ResourceConnection\Config\Proxy" />
@@ -741,6 +741,7 @@
741741
<argument name="fileSource" xsi:type="object">Magento\Framework\View\Layout\File\Collector\Aggregated\Proxy</argument>
742742
<argument name="pageLayoutFileSource" xsi:type="object">pageLayoutFileCollectorAggregated</argument>
743743
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Layout</argument>
744+
<argument name="layoutCacheKey" xsi:type="object">Magento\Framework\View\Layout\LayoutCacheKeyInterface</argument>
744745
</arguments>
745746
</type>
746747
<type name="CSSmin">

lib/internal/Magento/Framework/View/Layout/CacheKeyInterface.php

-26
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\View\Layout;
7+
8+
/**
9+
* Interface LayoutCacheKeyInterface
10+
*/
11+
interface LayoutCacheKeyInterface
12+
{
13+
/**
14+
* Add cache key(s) for generating different cache id for same handles
15+
*
16+
* @param array|string $cacheKeys
17+
* @return void
18+
*/
19+
public function addCacheKeys($cacheKeys);
20+
21+
/**
22+
* Return cache keys array
23+
*
24+
* @return array
25+
*/
26+
public function getCacheKeys();
27+
}

lib/internal/Magento/Framework/View/Model/Layout/CacheKey.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Layout cache key model
1010
*/
11-
class CacheKey implements \Magento\Framework\View\Layout\CacheKeyInterface
11+
class CacheKey implements \Magento\Framework\View\Layout\LayoutCacheKeyInterface
1212
{
1313
/**
1414
* Cache keys to be able to generate different cache id for same handles
@@ -21,8 +21,9 @@ class CacheKey implements \Magento\Framework\View\Layout\CacheKeyInterface
2121
* Add cache key(s) for generating different cache id for same handles
2222
*
2323
* @param array|string $cacheKeys
24+
* @return void
2425
*/
25-
public function addCacheKey($cacheKeys)
26+
public function addCacheKeys($cacheKeys)
2627
{
2728
if (!is_array($cacheKeys)) {
2829
$cacheKeys = [$cacheKeys];
@@ -31,7 +32,7 @@ public function addCacheKey($cacheKeys)
3132
}
3233

3334
/**
34-
* Return cache keys array stored
35+
* Return cache keys array
3536
*
3637
* @return array
3738
*/

lib/internal/Magento/Framework/View/Model/Layout/Merge.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\Config\Dom\ValidationException;
1010
use Magento\Framework\Filesystem\DriverPool;
1111
use Magento\Framework\Filesystem\File\ReadFactory;
12+
use Magento\Framework\View\Layout\LayoutCacheKeyInterface;
1213
use Magento\Framework\View\Model\Layout\Update\Validator;
1314

1415
/**
@@ -106,9 +107,9 @@ class Merge implements \Magento\Framework\View\Layout\ProcessorInterface
106107
/**
107108
* Cache keys to be able to generate different cache id for same handles
108109
*
109-
* @var \Magento\Framework\View\Layout\CacheKeyInterface
110+
* @var LayoutCacheKeyInterface
110111
*/
111-
private $cacheKey;
112+
private $layoutCacheKey;
112113

113114
/**
114115
* @var \Magento\Framework\Cache\FrontendInterface
@@ -174,8 +175,8 @@ class Merge implements \Magento\Framework\View\Layout\ProcessorInterface
174175
* @param \Psr\Log\LoggerInterface $logger
175176
* @param ReadFactory $readFactory ,
176177
* @param \Magento\Framework\View\Design\ThemeInterface $theme Non-injectable theme instance
177-
* @param \Magento\Framework\View\Layout\CacheKeyInterface $cacheKey
178178
* @param string $cacheSuffix
179+
* @param LayoutCacheKeyInterface $layoutCacheKey
179180
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
180181
*/
181182
public function __construct(
@@ -189,8 +190,8 @@ public function __construct(
189190
\Psr\Log\LoggerInterface $logger,
190191
ReadFactory $readFactory,
191192
\Magento\Framework\View\Design\ThemeInterface $theme = null,
192-
\Magento\Framework\View\Layout\CacheKeyInterface $cacheKey,
193-
$cacheSuffix = ''
193+
$cacheSuffix = '',
194+
LayoutCacheKeyInterface $layoutCacheKey = null
194195
) {
195196
$this->theme = $theme ?: $design->getDesignTheme();
196197
$this->scope = $scopeResolver->getScope();
@@ -201,8 +202,9 @@ public function __construct(
201202
$this->layoutValidator = $validator;
202203
$this->logger = $logger;
203204
$this->readFactory = $readFactory;
204-
$this->cacheKey = $cacheKey;
205205
$this->cacheSuffix = $cacheSuffix;
206+
$this->layoutCacheKey = $layoutCacheKey
207+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(LayoutCacheKeyInterface::class);
206208
}
207209

208210
/**
@@ -924,6 +926,6 @@ public function getScope()
924926
*/
925927
public function getCacheId()
926928
{
927-
return $this->generateCacheId(md5(implode('|', array_merge($this->getHandles(), $this->cacheKey->getCacheKeys()))));
929+
return $this->generateCacheId(md5(implode('|', array_merge($this->getHandles(), $this->layoutCacheKey->getCacheKeys()))));
928930
}
929931
}

0 commit comments

Comments
 (0)