Skip to content

Commit

Permalink
Merge pull request #400 from magento-frontend/MAGETWO-55594-2.2
Browse files Browse the repository at this point in the history
Bugs
MAGETWO-55594 Urgent - Opening product in Admin is very slow
  • Loading branch information
VladimirZaets authored Sep 20, 2016
2 parents d9a3833 + cfb571f commit 73bf648
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Categories;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\DB\Helper as DbHelper;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\Store;
Expand Down Expand Up @@ -112,4 +113,38 @@ public function testModifyMeta()

$this->assertArrayHasKey($groupCode, $this->getModel()->modifyMeta($meta));
}

public function testModifyMetaWithCaching()
{
$this->arrayManagerMock->expects($this->exactly(2))
->method('findPath')
->willReturn(true);
$cacheManager = $this->getMockBuilder(CacheInterface::class)
->getMockForAbstractClass();
$cacheManager->expects($this->once())
->method('load')
->with(Categories::CATEGORY_TREE_ID . '_');
$cacheManager->expects($this->once())
->method('save');

$modifier = $this->createModel();
$cacheContextProperty = new \ReflectionProperty(
Categories::class,
'cacheManager'
);
$cacheContextProperty->setAccessible(true);
$cacheContextProperty->setValue($modifier, $cacheManager);

$groupCode = 'test_group_code';
$meta = [
$groupCode => [
'children' => [
'category_ids' => [
'sortOrder' => 10,
],
],
],
];
$modifier->modifyMeta($meta);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@

use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\DB\Helper as DbHelper;
use Magento\Catalog\Model\Category as CategoryModel;
use Magento\Framework\UrlInterface;
use Magento\Framework\Stdlib\ArrayManager;

/**
* Data provider for categories field of product page
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Categories extends AbstractModifier
{
/**#@+
* Category tree cache id
*/
const CATEGORY_TREE_ID = 'CATALOG_PRODUCT_CATEGORY_TREE';
/**#@-*/

/**
* @var CategoryCollectionFactory
*/
Expand All @@ -29,6 +38,7 @@ class Categories extends AbstractModifier

/**
* @var array
* @deprecated
*/
protected $categoriesTrees = [];

Expand All @@ -47,6 +57,11 @@ class Categories extends AbstractModifier
*/
protected $arrayManager;

/**
* @var CacheInterface
*/
private $cacheManager;

/**
* @param LocatorInterface $locator
* @param CategoryCollectionFactory $categoryCollectionFactory
Expand All @@ -68,6 +83,21 @@ public function __construct(
$this->arrayManager = $arrayManager;
}

/**
* Retrieve cache interface
*
* @return CacheInterface
* @deprecated
*/
private function getCacheManager()
{
if (!$this->cacheManager) {
$this->cacheManager = ObjectManager::getInstance()
->get(CacheInterface::class);
}
return $this->cacheManager;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -254,8 +284,9 @@ protected function customizeCategoriesField(array $meta)
*/
protected function getCategoriesTree($filter = null)
{
if (isset($this->categoriesTrees[$filter])) {
return $this->categoriesTrees[$filter];
$categoryTree = $this->getCacheManager()->load(self::CATEGORY_TREE_ID . '_' . $filter);
if ($categoryTree) {
return unserialize($categoryTree);
}

$storeId = $this->locator->getStore()->getId();
Expand Down Expand Up @@ -307,9 +338,16 @@ protected function getCategoriesTree($filter = null)
$categoryById[$category->getId()]['label'] = $category->getName();
$categoryById[$category->getParentId()]['optgroup'][] = &$categoryById[$category->getId()];
}

$this->getCacheManager()->save(
serialize($categoryById[CategoryModel::TREE_ROOT_ID]['optgroup']),
self::CATEGORY_TREE_ID . '_' . $filter,
[
\Magento\Catalog\Model\Category::CACHE_TAG,
\Magento\Framework\App\Cache\Type\Block::CACHE_TAG
]
);

$this->categoriesTrees[$filter] = $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];

return $this->categoriesTrees[$filter];
return $categoryById[CategoryModel::TREE_ROOT_ID]['optgroup'];
}
}
28 changes: 27 additions & 1 deletion app/code/Magento/Ui/view/base/web/js/form/components/fieldset.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ define([
level: 0,
visible: true,
disabled: false,
listens: {
'opened': 'onVisibilityChange'
},
additionalClasses: {}
},

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

return this._super()
._setClasses();
._setClasses();
},

/**
* Initializes components' configuration.
*
* @returns {Fieldset} Chainable.
*/
initConfig: function () {
this._super();
this._wasOpened = this.opened || !this.collapsible;

return this;
},

/**
Expand Down Expand Up @@ -117,6 +132,17 @@ define([
return this;
},

/**
* Handler of the "opened" property changes.
*
* @param {Boolean} isOpened
*/
onVisibilityChange: function (isOpened) {
if (!this._wasOpened) {
this._wasOpened = isOpened;
}
},

/**
* Is being invoked on children validation error.
* Sets error property to one incoming.
Expand Down
Loading

0 comments on commit 73bf648

Please sign in to comment.