Skip to content

Commit 1dd2248

Browse files
committed
Merge branch 'MC-37417' of https://github.com/magento-mpi/magento2ce into PR-09-24-2020
2 parents 5977749 + ed139a6 commit 1dd2248

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra
3535
*/
3636
const LAYOUT_NODE = '<layout/>';
3737

38+
/**
39+
* Default cache life time
40+
*/
41+
private const DEFAULT_CACHE_LIFETIME = 31536000;
42+
3843
/**
3944
* Layout Update module
4045
*
@@ -172,6 +177,10 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra
172177
* @var SerializerInterface
173178
*/
174179
private $serializer;
180+
/**
181+
* @var int
182+
*/
183+
private $cacheLifetime;
175184

176185
/**
177186
* @param Layout\ProcessorFactory $processorFactory
@@ -188,6 +197,7 @@ class Layout extends \Magento\Framework\Simplexml\Config implements \Magento\Fra
188197
* @param \Psr\Log\LoggerInterface $logger
189198
* @param bool $cacheable
190199
* @param SerializerInterface|null $serializer
200+
* @param int|null $cacheLifetime
191201
*/
192202
public function __construct(
193203
Layout\ProcessorFactory $processorFactory,
@@ -203,7 +213,8 @@ public function __construct(
203213
AppState $appState,
204214
Logger $logger,
205215
$cacheable = true,
206-
SerializerInterface $serializer = null
216+
SerializerInterface $serializer = null,
217+
?int $cacheLifetime = null
207218
) {
208219
$this->_elementClass = \Magento\Framework\View\Layout\Element::class;
209220
$this->_renderingOutput = new \Magento\Framework\DataObject();
@@ -222,6 +233,7 @@ public function __construct(
222233
$this->generatorContextFactory = $generatorContextFactory;
223234
$this->appState = $appState;
224235
$this->logger = $logger;
236+
$this->cacheLifetime = $cacheLifetime ?? self::DEFAULT_CACHE_LIFETIME;
225237
}
226238

227239
/**
@@ -338,7 +350,8 @@ public function generateElements()
338350
'pageConfigStructure' => $this->getReaderContext()->getPageConfigStructure()->__toArray(),
339351
'scheduledStructure' => $this->getReaderContext()->getScheduledStructure()->__toArray(),
340352
];
341-
$this->cache->save($this->serializer->serialize($data), $cacheId, $this->getUpdate()->getHandles());
353+
$handles = $this->getUpdate()->getHandles();
354+
$this->cache->save($this->serializer->serialize($data), $cacheId, $handles, $this->cacheLifetime);
342355
}
343356

344357
$generatorContext = $this->generatorContextFactory->create(

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class Merge implements \Magento\Framework\View\Layout\ProcessorInterface
4747
*/
4848
const PAGE_LAYOUT_CACHE_SUFFIX = 'page_layout_merged';
4949

50+
/**
51+
* Default cache life time
52+
*/
53+
private const DEFAULT_CACHE_LIFETIME = 31536000;
54+
5055
/**
5156
* @var \Magento\Framework\View\Design\ThemeInterface
5257
*/
@@ -169,6 +174,10 @@ class Merge implements \Magento\Framework\View\Layout\ProcessorInterface
169174
* @var ReadFactory
170175
*/
171176
private $readFactory;
177+
/**
178+
* @var int
179+
*/
180+
private $cacheLifetime;
172181

173182
/**
174183
* Init merge model
@@ -182,10 +191,11 @@ class Merge implements \Magento\Framework\View\Layout\ProcessorInterface
182191
* @param \Magento\Framework\View\Model\Layout\Update\Validator $validator
183192
* @param \Psr\Log\LoggerInterface $logger
184193
* @param ReadFactory $readFactory
185-
* @param \Magento\Framework\View\Design\ThemeInterface $theme Non-injectable theme instance
194+
* @param \Magento\Framework\View\Design\ThemeInterface|null $theme Non-injectable theme instance
186195
* @param string $cacheSuffix
187-
* @param LayoutCacheKeyInterface $layoutCacheKey
196+
* @param LayoutCacheKeyInterface|null $layoutCacheKey
188197
* @param SerializerInterface|null $serializer
198+
* @param int|null $cacheLifetime
189199
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
190200
*/
191201
public function __construct(
@@ -201,7 +211,8 @@ public function __construct(
201211
\Magento\Framework\View\Design\ThemeInterface $theme = null,
202212
$cacheSuffix = '',
203213
LayoutCacheKeyInterface $layoutCacheKey = null,
204-
SerializerInterface $serializer = null
214+
SerializerInterface $serializer = null,
215+
?int $cacheLifetime = null
205216
) {
206217
$this->theme = $theme ?: $design->getDesignTheme();
207218
$this->scope = $scopeResolver->getScope();
@@ -216,6 +227,7 @@ public function __construct(
216227
$this->layoutCacheKey = $layoutCacheKey
217228
?: \Magento\Framework\App\ObjectManager::getInstance()->get(LayoutCacheKeyInterface::class);
218229
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
230+
$this->cacheLifetime = $cacheLifetime ?? self::DEFAULT_CACHE_LIFETIME;
219231
}
220232

221233
/**
@@ -749,7 +761,7 @@ protected function _loadCache($cacheId)
749761
*/
750762
protected function _saveCache($data, $cacheId, array $cacheTags = [])
751763
{
752-
$this->cache->save($data, $cacheId, $cacheTags, null);
764+
$this->cache->save($data, $cacheId, $cacheTags, $this->cacheLifetime);
753765
}
754766

755767
/**

lib/internal/Magento/Framework/View/Test/Unit/LayoutTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ public function testGenerateElementsWithoutCache(): void
944944

945945
$this->cacheMock->expects($this->once())
946946
->method('save')
947-
->with(json_encode($data), 'structure_' . $layoutCacheId, $handles)
947+
->with(json_encode($data), 'structure_' . $layoutCacheId, $handles, 31536000)
948948
->willReturn(true);
949949

950950
$generatorContextMock = $this->getMockBuilder(Context::class)

lib/internal/Magento/Framework/View/Test/Unit/Model/Layout/MergeTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\Serialize\SerializerInterface;
1515
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1616
use Magento\Framework\Url\ScopeInterface;
17+
use Magento\Framework\View\Design\ThemeInterface;
1718
use Magento\Framework\View\Layout\LayoutCacheKeyInterface;
1819
use Magento\Framework\View\Model\Layout\Merge;
1920
use Magento\Framework\View\Model\Layout\Update\Validator;
@@ -67,6 +68,10 @@ class MergeTest extends TestCase
6768
* @var LayoutCacheKeyInterface|MockObject
6869
*/
6970
protected $layoutCacheKeyMock;
71+
/**
72+
* @var ThemeInterface|MockObject
73+
*/
74+
private $theme;
7075

7176
protected function setUp(): void
7277
{
@@ -88,6 +93,8 @@ protected function setUp(): void
8893
->method('getCacheKeys')
8994
->willReturn([]);
9095

96+
$this->theme = $this->createMock(ThemeInterface::class);
97+
9198
$this->model = $this->objectManagerHelper->getObject(
9299
Merge::class,
93100
[
@@ -98,6 +105,7 @@ protected function setUp(): void
98105
'appState' => $this->appState,
99106
'layoutCacheKey' => $this->layoutCacheKeyMock,
100107
'serializer' => $this->serializer,
108+
'theme' => $this->theme,
101109
]
102110
);
103111
}
@@ -133,7 +141,12 @@ public function testValidateMergedLayoutThrowsException()
133141
public function testSaveToCache()
134142
{
135143
$this->scope->expects($this->once())->method('getId')->willReturn(1);
136-
$this->cache->expects($this->once())->method('save');
144+
$this->theme->method('getArea')->willReturn('frontend');
145+
$this->theme->method('getId')->willReturn(1);
146+
$cacheKey = 'LAYOUT_frontend_STORE1_1d41d8cd98f00b204e9800998ecf8427e_page_layout_merged';
147+
$this->cache->expects($this->once())
148+
->method('save')
149+
->with(null, $cacheKey, [], 31536000);
137150

138151
$this->model->load();
139152
}

0 commit comments

Comments
 (0)