Skip to content

Commit

Permalink
Fix PageCache: async rendering of blocks can corrupt layout cache mag…
Browse files Browse the repository at this point in the history
…ento#8554 magento#9050 magento#9560

Renamed interface, LayoutCacheKeyInterface made optional in constructor, injected via di.xml, some other little fixes
  • Loading branch information
adrian-martinez-interactiv4 committed Sep 22, 2017
1 parent bd6ed7c commit c6e2b39
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 44 deletions.
16 changes: 9 additions & 7 deletions app/code/Magento/PageCache/Controller/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Framework\Serialize\Serializer\Base64Json;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\View\Layout\LayoutCacheKeyInterface;

abstract class Block extends \Magento\Framework\App\Action\Action
{
Expand All @@ -30,36 +31,37 @@ abstract class Block extends \Magento\Framework\App\Action\Action
/**
* Layout cache keys to be able to generate different cache id for same handles
*
* @var \Magento\Framework\View\Layout\CacheKeyInterface
* @var LayoutCacheKeyInterface
*/
private $cacheKey;
private $layoutCacheKey;

/**
* @var string
*/
private $layoutCacheKey = 'mage_pagecache';
private $layoutCacheKeyName = 'mage_pagecache';

/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\Translate\InlineInterface $translateInline
* @param Json $jsonSerializer
* @param Base64Json $base64jsonSerializer
* @param \Magento\Framework\View\Layout\CacheKeyInterface $cacheKey
* @param LayoutCacheKeyInterface $layoutCacheKey
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\Translate\InlineInterface $translateInline,
Json $jsonSerializer = null,
Base64Json $base64jsonSerializer = null,
\Magento\Framework\View\Layout\CacheKeyInterface $cacheKey
LayoutCacheKeyInterface $layoutCacheKey = null
) {
parent::__construct($context);
$this->translateInline = $translateInline;
$this->jsonSerializer = $jsonSerializer
?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
$this->base64jsonSerializer = $base64jsonSerializer
?: \Magento\Framework\App\ObjectManager::getInstance()->get(Base64Json::class);
$this->cacheKey = $cacheKey;
$this->layoutCacheKey = $layoutCacheKey
?: \Magento\Framework\App\ObjectManager::getInstance()->get(LayoutCacheKeyInterface::class);
}

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

$layout = $this->_view->getLayout();
$this->cacheKey->addCacheKey($this->layoutCacheKey);
$this->layoutCacheKey->addCacheKeys($this->layoutCacheKeyName);

$this->_view->loadLayout($handles, true, true, false);
$data = [];
Expand Down
5 changes: 5 additions & 0 deletions app/code/Magento/PageCache/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
</argument>
</arguments>
</type>
<type name="Magento\PageCache\Controller\Block">
<arguments>
<argument name="layoutCacheKey" xsi:type="object">Magento\Framework\View\Layout\LayoutCacheKeyInterface</argument>
</arguments>
</type>
<preference for="Magento\PageCache\Model\VclGeneratorInterface" type="Magento\PageCache\Model\Varnish\VclGenerator"/>
<preference for="Magento\PageCache\Model\VclTemplateLocatorInterface" type="Magento\PageCache\Model\Varnish\VclTemplateLocator"/>
</config>
3 changes: 2 additions & 1 deletion app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<preference for="Magento\Framework\Event\ManagerInterface" type="Magento\Framework\Event\Manager\Proxy" />
<preference for="Magento\Framework\View\LayoutInterface" type="Magento\Framework\View\Layout" />
<preference for="Magento\Framework\View\Layout\ProcessorInterface" type="Magento\Framework\View\Model\Layout\Merge" />
<preference for="Magento\Framework\View\Layout\CacheKeyInterface" type="Magento\Framework\View\Model\Layout\CacheKey" />
<preference for="Magento\Framework\View\Layout\LayoutCacheKeyInterface" type="Magento\Framework\View\Model\Layout\CacheKey" />
<preference for="Magento\Framework\View\Url\ConfigInterface" type="Magento\Framework\View\Url\Config" />
<preference for="Magento\Framework\App\Route\ConfigInterface" type="Magento\Framework\App\Route\Config" />
<preference for="Magento\Framework\App\ResourceConnection\ConfigInterface" type="Magento\Framework\App\ResourceConnection\Config\Proxy" />
Expand Down Expand Up @@ -741,6 +741,7 @@
<argument name="fileSource" xsi:type="object">Magento\Framework\View\Layout\File\Collector\Aggregated\Proxy</argument>
<argument name="pageLayoutFileSource" xsi:type="object">pageLayoutFileCollectorAggregated</argument>
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Layout</argument>
<argument name="layoutCacheKey" xsi:type="object">Magento\Framework\View\Layout\LayoutCacheKeyInterface</argument>
</arguments>
</type>
<type name="CSSmin">
Expand Down
26 changes: 0 additions & 26 deletions lib/internal/Magento/Framework/View/Layout/CacheKeyInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Layout;

/**
* Interface LayoutCacheKeyInterface
*/
interface LayoutCacheKeyInterface
{
/**
* Add cache key(s) for generating different cache id for same handles
*
* @param array|string $cacheKeys
* @return void
*/
public function addCacheKeys($cacheKeys);

/**
* Return cache keys array
*
* @return array
*/
public function getCacheKeys();
}
7 changes: 4 additions & 3 deletions lib/internal/Magento/Framework/View/Model/Layout/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Layout cache key model
*/
class CacheKey implements \Magento\Framework\View\Layout\CacheKeyInterface
class CacheKey implements \Magento\Framework\View\Layout\LayoutCacheKeyInterface
{
/**
* Cache keys to be able to generate different cache id for same handles
Expand All @@ -21,8 +21,9 @@ class CacheKey implements \Magento\Framework\View\Layout\CacheKeyInterface
* Add cache key(s) for generating different cache id for same handles
*
* @param array|string $cacheKeys
* @return void
*/
public function addCacheKey($cacheKeys)
public function addCacheKeys($cacheKeys)
{
if (!is_array($cacheKeys)) {
$cacheKeys = [$cacheKeys];
Expand All @@ -31,7 +32,7 @@ public function addCacheKey($cacheKeys)
}

/**
* Return cache keys array stored
* Return cache keys array
*
* @return array
*/
Expand Down
16 changes: 9 additions & 7 deletions lib/internal/Magento/Framework/View/Model/Layout/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\Config\Dom\ValidationException;
use Magento\Framework\Filesystem\DriverPool;
use Magento\Framework\Filesystem\File\ReadFactory;
use Magento\Framework\View\Layout\LayoutCacheKeyInterface;
use Magento\Framework\View\Model\Layout\Update\Validator;

/**
Expand Down Expand Up @@ -106,9 +107,9 @@ class Merge implements \Magento\Framework\View\Layout\ProcessorInterface
/**
* Cache keys to be able to generate different cache id for same handles
*
* @var \Magento\Framework\View\Layout\CacheKeyInterface
* @var LayoutCacheKeyInterface
*/
private $cacheKey;
private $layoutCacheKey;

/**
* @var \Magento\Framework\Cache\FrontendInterface
Expand Down Expand Up @@ -174,8 +175,8 @@ class Merge implements \Magento\Framework\View\Layout\ProcessorInterface
* @param \Psr\Log\LoggerInterface $logger
* @param ReadFactory $readFactory ,
* @param \Magento\Framework\View\Design\ThemeInterface $theme Non-injectable theme instance
* @param \Magento\Framework\View\Layout\CacheKeyInterface $cacheKey
* @param string $cacheSuffix
* @param LayoutCacheKeyInterface $layoutCacheKey
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -189,8 +190,8 @@ public function __construct(
\Psr\Log\LoggerInterface $logger,
ReadFactory $readFactory,
\Magento\Framework\View\Design\ThemeInterface $theme = null,
\Magento\Framework\View\Layout\CacheKeyInterface $cacheKey,
$cacheSuffix = ''
$cacheSuffix = '',
LayoutCacheKeyInterface $layoutCacheKey = null
) {
$this->theme = $theme ?: $design->getDesignTheme();
$this->scope = $scopeResolver->getScope();
Expand All @@ -201,8 +202,9 @@ public function __construct(
$this->layoutValidator = $validator;
$this->logger = $logger;
$this->readFactory = $readFactory;
$this->cacheKey = $cacheKey;
$this->cacheSuffix = $cacheSuffix;
$this->layoutCacheKey = $layoutCacheKey
?: \Magento\Framework\App\ObjectManager::getInstance()->get(LayoutCacheKeyInterface::class);
}

/**
Expand Down Expand Up @@ -924,6 +926,6 @@ public function getScope()
*/
public function getCacheId()
{
return $this->generateCacheId(md5(implode('|', array_merge($this->getHandles(), $this->cacheKey->getCacheKeys()))));
return $this->generateCacheId(md5(implode('|', array_merge($this->getHandles(), $this->layoutCacheKey->getCacheKeys()))));
}
}

0 comments on commit c6e2b39

Please sign in to comment.