From c6e2b399166ede65bc2c19cc7ed83d35b371b01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Mart=C3=ADnez?= Date: Fri, 22 Sep 2017 02:36:39 +0200 Subject: [PATCH] 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 --- .../Magento/PageCache/Controller/Block.php | 16 ++++++----- app/code/Magento/PageCache/etc/di.xml | 5 ++++ app/etc/di.xml | 3 ++- .../View/Layout/CacheKeyInterface.php | 26 ------------------ .../View/Layout/LayoutCacheKeyInterface.php | 27 +++++++++++++++++++ .../Framework/View/Model/Layout/CacheKey.php | 7 ++--- .../Framework/View/Model/Layout/Merge.php | 16 ++++++----- 7 files changed, 56 insertions(+), 44 deletions(-) delete mode 100644 lib/internal/Magento/Framework/View/Layout/CacheKeyInterface.php create mode 100644 lib/internal/Magento/Framework/View/Layout/LayoutCacheKeyInterface.php diff --git a/app/code/Magento/PageCache/Controller/Block.php b/app/code/Magento/PageCache/Controller/Block.php index 6a5a502e6d570..e69614496c66d 100644 --- a/app/code/Magento/PageCache/Controller/Block.php +++ b/app/code/Magento/PageCache/Controller/Block.php @@ -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 { @@ -30,28 +31,28 @@ 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; @@ -59,7 +60,8 @@ public function __construct( ?: \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); } /** @@ -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 = []; diff --git a/app/code/Magento/PageCache/etc/di.xml b/app/code/Magento/PageCache/etc/di.xml index 92daefe046213..2a9abbb860805 100644 --- a/app/code/Magento/PageCache/etc/di.xml +++ b/app/code/Magento/PageCache/etc/di.xml @@ -32,6 +32,11 @@ + + + Magento\Framework\View\Layout\LayoutCacheKeyInterface + + diff --git a/app/etc/di.xml b/app/etc/di.xml index 84c74fd42dd07..33e27305b78b9 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -68,7 +68,7 @@ - + @@ -741,6 +741,7 @@ Magento\Framework\View\Layout\File\Collector\Aggregated\Proxy pageLayoutFileCollectorAggregated Magento\Framework\App\Cache\Type\Layout + Magento\Framework\View\Layout\LayoutCacheKeyInterface diff --git a/lib/internal/Magento/Framework/View/Layout/CacheKeyInterface.php b/lib/internal/Magento/Framework/View/Layout/CacheKeyInterface.php deleted file mode 100644 index a0bb31c7c4f98..0000000000000 --- a/lib/internal/Magento/Framework/View/Layout/CacheKeyInterface.php +++ /dev/null @@ -1,26 +0,0 @@ -theme = $theme ?: $design->getDesignTheme(); $this->scope = $scopeResolver->getScope(); @@ -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); } /** @@ -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())))); } }