Skip to content

Commit 24c3009

Browse files
committed
MC-33556: [PB] Degradation in product Edit/Save scenarios
1 parent f6cf95b commit 24c3009

File tree

3 files changed

+136
-12
lines changed

3 files changed

+136
-12
lines changed

app/code/Magento/PageBuilder/Block/WysiwygSetup.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,41 @@
88

99
namespace Magento\PageBuilder\Block;
1010

11-
use Magento\Framework\View\Element\Template;
11+
use Magento\Framework\Cache\FrontendInterface;
1212
use Magento\Framework\DataObject;
13+
use Magento\Framework\View\Element\Template;
1314

1415
/**
1516
* @api
1617
*/
1718
class WysiwygSetup extends Template
1819
{
20+
private const WYSIWYG_CONFIG_CACHE_ID = 'WYSIWYG_CONFIG';
21+
1922
/**
2023
* @var \Magento\Ui\Component\Wysiwyg\ConfigInterface
2124
*/
2225
private $config;
2326

27+
/**
28+
* @var FrontendInterface
29+
*/
30+
private $cache;
31+
2432
/**
2533
* @param Template\Context $context
2634
* @param \Magento\Ui\Component\Wysiwyg\ConfigInterface $config
2735
* @param array $data
36+
* @param FrontendInterface|null $cache
2837
*/
2938
public function __construct(
3039
\Magento\Framework\View\Element\Template\Context $context,
3140
\Magento\Ui\Component\Wysiwyg\ConfigInterface $config,
32-
array $data = []
41+
array $data = [],
42+
FrontendInterface $cache = null
3343
) {
3444
$this->config = $config;
45+
$this->cache = $cache ?: \Magento\Framework\App\ObjectManager::getInstance()->get(FrontendInterface::class);
3546
parent::__construct($context, $data);
3647
}
3748

@@ -42,12 +53,16 @@ public function __construct(
4253
*/
4354
public function getConfigJson() : string
4455
{
45-
$config = $this->config->getConfig();
46-
47-
if (is_array($config)) {
48-
$config = new DataObject($config);
56+
$configJson = $this->cache->load(self::WYSIWYG_CONFIG_CACHE_ID);
57+
if (!$configJson) {
58+
$config = $this->config->getConfig();
59+
if (is_array($config)) {
60+
$config = new DataObject($config);
61+
}
62+
$configJson = $config->toJson();
63+
$this->cache->save($configJson, self::WYSIWYG_CONFIG_CACHE_ID);
4964
}
5065

51-
return $config->toJson();
66+
return $configJson;
5267
}
5368
}

app/code/Magento/PageBuilder/Model/Stage/Config.php

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Framework\App\ObjectManager;
1212
use Magento\Framework\UrlInterface;
1313
use Magento\Framework\AuthorizationInterface;
14+
use Magento\Framework\Cache\FrontendInterface;
15+
use Magento\Framework\Serialize\Serializer\Json;
1416

1517
/**
1618
* Provide configuration to the admin JavaScript app
@@ -32,6 +34,10 @@ class Config
3234
const TEMPLATE_SAVE_RESOURCE = 'Magento_PageBuilder::template_save';
3335
const TEMPLATE_APPLY_RESOURCE = 'Magento_PageBuilder::template_apply';
3436

37+
private const CONTENT_TYPE_CACHE_ID = 'CONTENT_TYPE';
38+
private const TINY_MCE_CONFIG_CACHE_ID = 'TYNY_MCE_CONFIG';
39+
private const WIDGET_BREAKPOINS_CACHE_ID = 'WIDGET_BREAKPOINS';
40+
3541
/**
3642
* @var \Magento\PageBuilder\Model\ConfigInterface
3743
*/
@@ -102,6 +108,16 @@ class Config
102108
*/
103109
private $authorization;
104110

111+
/**
112+
* @var FrontendInterface
113+
*/
114+
private $cache;
115+
116+
/**
117+
* @var Json
118+
*/
119+
private $serializer;
120+
105121
/**
106122
* @param \Magento\PageBuilder\Model\ConfigInterface $config
107123
* @param Config\UiComponentConfig $uiComponentConfig
@@ -117,6 +133,8 @@ class Config
117133
* @param \Magento\Widget\Model\Widget\Config|null $widgetConfig
118134
* @param \Magento\Variable\Model\Variable\Config|null $variableConfig
119135
* @param AuthorizationInterface|null $authorization
136+
* @param FrontendInterface|null $cache
137+
* @param Json|null $serializer
120138
*
121139
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
122140
*/
@@ -134,7 +152,9 @@ public function __construct(
134152
array $data = [],
135153
\Magento\Widget\Model\Widget\Config $widgetConfig = null,
136154
\Magento\Variable\Model\Variable\Config $variableConfig = null,
137-
AuthorizationInterface $authorization = null
155+
AuthorizationInterface $authorization = null,
156+
FrontendInterface $cache = null,
157+
Json $serializer = null
138158
) {
139159
$this->config = $config;
140160
$this->uiComponentConfig = $uiComponentConfig;
@@ -152,6 +172,8 @@ public function __construct(
152172
$this->variableConfig = $variableConfig ?? \Magento\Framework\App\ObjectManager::getInstance()
153173
->get(\Magento\Variable\Model\Variable\Config::class);
154174
$this->authorization = $authorization ?: ObjectManager::getInstance()->get(AuthorizationInterface::class);
175+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
176+
$this->cache = $cache ?: \Magento\Framework\App\ObjectManager::getInstance()->get(FrontendInterface::class);
155177
}
156178

157179
/**
@@ -173,8 +195,8 @@ public function getConfig()
173195
'column_grid_max' => $this->scopeConfig->getValue(self::XML_PATH_COLUMN_GRID_MAX),
174196
'can_use_inline_editing_on_stage' => $this->isWysiwygProvisionedForEditingOnStage(),
175197
'widgets' => $this->widgetInitializerConfig->getConfig(),
176-
'breakpoints' => $this->widgetInitializerConfig->getBreakpoints(),
177-
'tinymce' => $this->getTinyMceConfig(),
198+
'breakpoints' => $this->getCachedWidgetBreakpoints(),
199+
'tinymce' => $this->getCachedTinyMceConfig(),
178200
'acl' => $this->getAcl()
179201
];
180202
}
@@ -243,21 +265,45 @@ private function getContentTypes()
243265

244266
$contentTypeData = [];
245267
foreach ($contentTypes as $name => $contentType) {
246-
$contentTypeData[$name] = $this->flattenContentTypeData(
268+
$contentTypeData[$name] = $this->getCachedFlattenContentTypeData(
247269
$name,
248270
$contentType
249271
);
250272
}
251273

252274
// The stage requires a root container to house it's children
253-
$contentTypeData[self::ROOT_CONTAINER_NAME] = $this->flattenContentTypeData(
275+
$contentTypeData[self::ROOT_CONTAINER_NAME] = $this->getCachedFlattenContentTypeData(
254276
self::ROOT_CONTAINER_NAME,
255277
$this->rootContainerConfig
256278
);
257279

258280
return $contentTypeData;
259281
}
260282

283+
/**
284+
* Get flatten content type for content name from cache and add it to cache if wasn't cached
285+
*
286+
* @param string $name
287+
* @param array $contentType
288+
*
289+
* @return array
290+
*/
291+
private function getCachedFlattenContentTypeData(string $name, array $contentType)
292+
{
293+
$identifier = self::CONTENT_TYPE_CACHE_ID . '_' . $name;
294+
295+
$flattenContentTypeData = $this->getCache($identifier);
296+
if (empty($flattenContentTypeData)) {
297+
$flattenContentTypeData = $this->flattenContentTypeData(
298+
$name,
299+
$contentType
300+
);
301+
$this->saveCache($flattenContentTypeData, $identifier);
302+
}
303+
304+
return $flattenContentTypeData;
305+
}
306+
261307
/**
262308
* Flatten the content type
263309
*
@@ -308,4 +354,57 @@ private function isWysiwygProvisionedForEditingOnStage()
308354

309355
return $this->inlineEditingChecker->isSupported($activeEditorPath);
310356
}
357+
358+
/**
359+
* Get the TINY MCE config from cache and add it to cache if it wasn't cached
360+
*
361+
* @return array
362+
*/
363+
private function getCachedTinyMceConfig(): array
364+
{
365+
$configData = $this->getCache(self::TINY_MCE_CONFIG_CACHE_ID);
366+
if (empty($configData)) {
367+
$configData = $this->getTinyMceConfig();
368+
$this->saveCache($configData, self::TINY_MCE_CONFIG_CACHE_ID);
369+
}
370+
return $configData;
371+
}
372+
373+
/**
374+
* Get widget breakpoints from cache and add it to cache if it wasn't cached
375+
*
376+
* @return array
377+
*/
378+
private function getCachedWidgetBreakpoints(): array
379+
{
380+
$cache = $this->getCache(self::WIDGET_BREAKPOINS_CACHE_ID);
381+
if (empty($cache)) {
382+
$cache = $this->widgetInitializerConfig->getBreakpoints();
383+
$this->saveCache($cache, self::WIDGET_BREAKPOINS_CACHE_ID);
384+
}
385+
return $cache;
386+
}
387+
388+
/**
389+
* @param $cacheIdentifier
390+
* @return array
391+
*/
392+
private function getCache($cacheIdentifier): array
393+
{
394+
$serializedData = $this->cache->load($cacheIdentifier);
395+
$cache = $serializedData
396+
? $this->serializer->unserialize($serializedData)
397+
: [];
398+
399+
return $cache;
400+
}
401+
402+
/**
403+
* @param array $data
404+
* @param $cacheIdentifier
405+
*/
406+
private function saveCache(array $data, $cacheIdentifier): void
407+
{
408+
$this->cache->save($this->serializer->serialize($data), $cacheIdentifier);
409+
}
311410
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
<preference for="Magento\PageBuilder\Model\Dom\Adapter\TextInterface" type="Magento\PageBuilder\Model\Dom\Text" />
2727
<preference for="Magento\PageBuilder\Model\Dom\Adapter\TokenListInterface" type="Magento\PageBuilder\Model\Dom\TokenList" />
2828
<preference for="Magento\PageBuilder\Model\Dom\Adapter\XmlDocumentInterface" type="Magento\PageBuilder\Model\Dom\XmlDocument" />
29+
<type name="Magento\PageBuilder\Model\Stage\Config">
30+
<arguments>
31+
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
32+
</arguments>
33+
</type>
34+
<type name="Magento\PageBuilder\Block\WysiwygSetup">
35+
<arguments>
36+
<argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Config</argument>
37+
</arguments>
38+
</type>
2939
<type name="Magento\PageBuilder\Model\Stage\Config\UiComponentConfig">
3040
<arguments>
3141
<argument name="configFactory" xsi:type="object">uiComponentConfigFactory</argument>

0 commit comments

Comments
 (0)