Skip to content

Commit 73bf648

Browse files
Merge pull request #400 from magento-frontend/MAGETWO-55594-2.2
Bugs MAGETWO-55594 Urgent - Opening product in Admin is very slow
2 parents d9a3833 + cfb571f commit 73bf648

File tree

13 files changed

+383
-235
lines changed

13 files changed

+383
-235
lines changed

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/CategoriesTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Categories;
99
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1010
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
11+
use Magento\Framework\App\CacheInterface;
1112
use Magento\Framework\DB\Helper as DbHelper;
1213
use Magento\Framework\UrlInterface;
1314
use Magento\Store\Model\Store;
@@ -112,4 +113,38 @@ public function testModifyMeta()
112113

113114
$this->assertArrayHasKey($groupCode, $this->getModel()->modifyMeta($meta));
114115
}
116+
117+
public function testModifyMetaWithCaching()
118+
{
119+
$this->arrayManagerMock->expects($this->exactly(2))
120+
->method('findPath')
121+
->willReturn(true);
122+
$cacheManager = $this->getMockBuilder(CacheInterface::class)
123+
->getMockForAbstractClass();
124+
$cacheManager->expects($this->once())
125+
->method('load')
126+
->with(Categories::CATEGORY_TREE_ID . '_');
127+
$cacheManager->expects($this->once())
128+
->method('save');
129+
130+
$modifier = $this->createModel();
131+
$cacheContextProperty = new \ReflectionProperty(
132+
Categories::class,
133+
'cacheManager'
134+
);
135+
$cacheContextProperty->setAccessible(true);
136+
$cacheContextProperty->setValue($modifier, $cacheManager);
137+
138+
$groupCode = 'test_group_code';
139+
$meta = [
140+
$groupCode => [
141+
'children' => [
142+
'category_ids' => [
143+
'sortOrder' => 10,
144+
],
145+
],
146+
],
147+
];
148+
$modifier->modifyMeta($meta);
149+
}
115150
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Categories.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@
77

88
use Magento\Catalog\Model\Locator\LocatorInterface;
99
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\App\CacheInterface;
1012
use Magento\Framework\DB\Helper as DbHelper;
1113
use Magento\Catalog\Model\Category as CategoryModel;
1214
use Magento\Framework\UrlInterface;
1315
use Magento\Framework\Stdlib\ArrayManager;
1416

1517
/**
1618
* Data provider for categories field of product page
19+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1720
*/
1821
class Categories extends AbstractModifier
1922
{
23+
/**#@+
24+
* Category tree cache id
25+
*/
26+
const CATEGORY_TREE_ID = 'CATALOG_PRODUCT_CATEGORY_TREE';
27+
/**#@-*/
28+
2029
/**
2130
* @var CategoryCollectionFactory
2231
*/
@@ -29,6 +38,7 @@ class Categories extends AbstractModifier
2938

3039
/**
3140
* @var array
41+
* @deprecated
3242
*/
3343
protected $categoriesTrees = [];
3444

@@ -47,6 +57,11 @@ class Categories extends AbstractModifier
4757
*/
4858
protected $arrayManager;
4959

60+
/**
61+
* @var CacheInterface
62+
*/
63+
private $cacheManager;
64+
5065
/**
5166
* @param LocatorInterface $locator
5267
* @param CategoryCollectionFactory $categoryCollectionFactory
@@ -68,6 +83,21 @@ public function __construct(
6883
$this->arrayManager = $arrayManager;
6984
}
7085

86+
/**
87+
* Retrieve cache interface
88+
*
89+
* @return CacheInterface
90+
* @deprecated
91+
*/
92+
private function getCacheManager()
93+
{
94+
if (!$this->cacheManager) {
95+
$this->cacheManager = ObjectManager::getInstance()
96+
->get(CacheInterface::class);
97+
}
98+
return $this->cacheManager;
99+
}
100+
71101
/**
72102
* {@inheritdoc}
73103
*/
@@ -254,8 +284,9 @@ protected function customizeCategoriesField(array $meta)
254284
*/
255285
protected function getCategoriesTree($filter = null)
256286
{
257-
if (isset($this->categoriesTrees[$filter])) {
258-
return $this->categoriesTrees[$filter];
287+
$categoryTree = $this->getCacheManager()->load(self::CATEGORY_TREE_ID . '_' . $filter);
288+
if ($categoryTree) {
289+
return unserialize($categoryTree);
259290
}
260291

261292
$storeId = $this->locator->getStore()->getId();
@@ -307,9 +338,16 @@ protected function getCategoriesTree($filter = null)
307338
$categoryById[$category->getId()]['label'] = $category->getName();
308339
$categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()];
309340
}
341+
342+
$this->getCacheManager()->save(
343+
serialize($categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']),
344+
self::CATEGORY_TREE_ID . '_' . $filter,
345+
[
346+
\Magento\Catalog\Model\Category::CACHE_TAG,
347+
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
348+
]
349+
);
310350

311-
$this->categoriesTrees[$filter] = $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];
312-
313-
return $this->categoriesTrees[$filter];
351+
return $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];
314352
}
315353
}

app/code/Magento/Ui/view/base/web/js/form/components/fieldset.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ define([
1919
level: 0,
2020
visible: true,
2121
disabled: false,
22+
listens: {
23+
'opened': 'onVisibilityChange'
24+
},
2225
additionalClasses: {}
2326
},
2427

@@ -30,7 +33,19 @@ define([
3033
_.bindAll(this, 'onChildrenUpdate', 'onChildrenError', 'onContentLoading');
3134

3235
return this._super()
33-
._setClasses();
36+
._setClasses();
37+
},
38+
39+
/**
40+
* Initializes components' configuration.
41+
*
42+
* @returns {Fieldset} Chainable.
43+
*/
44+
initConfig: function () {
45+
this._super();
46+
this._wasOpened = this.opened || !this.collapsible;
47+
48+
return this;
3449
},
3550

3651
/**
@@ -117,6 +132,17 @@ define([
117132
return this;
118133
},
119134

135+
/**
136+
* Handler of the "opened" property changes.
137+
*
138+
* @param {Boolean} isOpened
139+
*/
140+
onVisibilityChange: function (isOpened) {
141+
if (!this._wasOpened) {
142+
this._wasOpened = isOpened;
143+
}
144+
},
145+
120146
/**
121147
* Is being invoked on children validation error.
122148
* Sets error property to one incoming.

0 commit comments

Comments
 (0)