Skip to content

Commit a01dc43

Browse files
committed
MC-38593: Complex products children ids are not cached in Type class leading to multiple unnecessary db requests
1 parent bd51f4d commit a01dc43

File tree

10 files changed

+108
-179
lines changed

10 files changed

+108
-179
lines changed

app/code/Magento/Bundle/Model/Plugin/Frontend/ProductIdentitiesExtender.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class ProductIdentitiesExtender
2020
*/
2121
private $type;
2222

23+
/**
24+
* @var array
25+
*/
26+
private $cacheChildrenIds = [];
27+
2328
/**
2429
* @param BundleType $type
2530
*/
@@ -40,12 +45,27 @@ public function afterGetIdentities(CatalogProduct $product, array $identities):
4045
if ($product->getTypeId() !== BundleType::TYPE_CODE) {
4146
return $identities;
4247
}
43-
foreach ($this->type->getChildrenIds($product->getEntityId()) as $childIds) {
48+
foreach ($this->getChildrenIds($product->getEntityId()) as $childIds) {
4449
foreach ($childIds as $childId) {
4550
$identities[] = CatalogProduct::CACHE_TAG . '_' . $childId;
4651
}
4752
}
4853

4954
return array_unique($identities);
5055
}
56+
57+
/**
58+
* Get children ids with cache use
59+
*
60+
* @param mixed $entityId
61+
* @return array
62+
*/
63+
private function getChildrenIds($entityId)
64+
{
65+
if (!isset($this->cacheChildrenIds[$entityId])) {
66+
$this->cacheChildrenIds[$entityId] = $this->type->getChildrenIds($entityId);
67+
}
68+
69+
return $this->cacheChildrenIds[$entityId];
70+
}
5171
}

app/code/Magento/Bundle/Model/Plugin/ProductIdentitiesExtender.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class ProductIdentitiesExtender
1717
*/
1818
private $type;
1919

20+
/**
21+
* @var array
22+
*/
23+
private $cacheParentIdsByChild = [];
24+
2025
/**
2126
* @param BundleType $type
2227
*/
@@ -39,9 +44,24 @@ public function afterGetIdentities(
3944
if ($product->getTypeId() !== BundleType::TYPE_CODE) {
4045
return $identities;
4146
}
42-
foreach ($this->type->getParentIdsByChild($product->getEntityId()) as $parentId) {
47+
foreach ($this->getParentIdsByChild($product->getEntityId()) as $parentId) {
4348
$identities[] = CatalogProduct::CACHE_TAG . '_' . $parentId;
4449
}
4550
return $identities;
4651
}
52+
53+
/**
54+
* Get parent ids by child with cache use
55+
*
56+
* @param mixed $entityId
57+
* @return array
58+
*/
59+
private function getParentIdsByChild($entityId)
60+
{
61+
if (!isset($this->cacheParentIdsByChild[$entityId])) {
62+
$this->cacheParentIdsByChild[$entityId] = $this->type->getParentIdsByChild($entityId);
63+
}
64+
65+
return $this->cacheParentIdsByChild[$entityId];
66+
}
4767
}

app/code/Magento/Bundle/Model/Product/Type.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,6 @@ class Type extends \Magento\Catalog\Model\Product\Type\AbstractType
168168
*/
169169
private $arrayUtility;
170170

171-
/**
172-
* @var array
173-
*/
174-
private $cacheChildrenIds = [];
175-
176-
/**
177-
* @var array
178-
*/
179-
private $cacheParentIdsByChild = [];
180-
181171
/**
182172
* @param \Magento\Catalog\Model\Product\Option $catalogProductOption
183173
* @param \Magento\Eav\Model\Config $eavConfig
@@ -295,12 +285,7 @@ public function getRelationInfo()
295285
*/
296286
public function getChildrenIds($parentId, $required = true)
297287
{
298-
$cacheKey = $parentId . '-' . ($required ? 1 : 0);
299-
if (!isset($this->cacheChildrenIds[$cacheKey])) {
300-
$this->cacheChildrenIds[$cacheKey] = $this->_bundleSelection->getChildrenIds($parentId, $required);
301-
}
302-
303-
return $this->cacheChildrenIds[$cacheKey];
288+
return $this->_bundleSelection->getChildrenIds($parentId, $required);
304289
}
305290

306291
/**
@@ -311,16 +296,7 @@ public function getChildrenIds($parentId, $required = true)
311296
*/
312297
public function getParentIdsByChild($childId)
313298
{
314-
if (is_array($childId) && count($childId) > 1) {
315-
return $this->_bundleSelection->getParentIdsByChild($childId);
316-
}
317-
318-
$cacheKey = is_array($childId) ? array_shift($childId) : $childId;
319-
if (!isset($this->cacheParentIdsByChild[$cacheKey])) {
320-
$this->cacheParentIdsByChild[$cacheKey] = $this->_bundleSelection->getParentIdsByChild($childId);
321-
}
322-
323-
return $this->cacheParentIdsByChild[$cacheKey];
299+
return $this->_bundleSelection->getParentIdsByChild($childId);
324300
}
325301

326302
/**

app/code/Magento/Bundle/Test/Unit/Model/Plugin/Frontend/ProductIdentitiesExtenderTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public function testAfterGetIdentities()
6262
Product::CACHE_TAG . '_' . 45,
6363
Product::CACHE_TAG . '_' . 24612,
6464
];
65-
$this->product->expects($this->once())
65+
$this->product->expects($this->exactly(2))
6666
->method('getEntityId')
6767
->willReturn($id);
68-
$this->product->expects($this->once())
68+
$this->product->expects($this->exactly(2))
6969
->method('getTypeId')
7070
->willReturn(Type::TYPE_CODE);
7171
$this->type->expects($this->once())
@@ -74,5 +74,12 @@ public function testAfterGetIdentities()
7474
->willReturn($childIds);
7575
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities);
7676
$this->assertEquals($expectedIdentities, $identities);
77+
78+
$this->type->expects($this->never())
79+
->method('getChildrenIds')
80+
->with($id)
81+
->willReturn($childIds);
82+
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities);
83+
$this->assertEquals($expectedIdentities, $identities);
7784
}
7885
}

app/code/Magento/Bundle/Test/Unit/Model/Plugin/ProductIdentitiesExtenderTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public function testAfterGetIdentities()
7070
Product::CACHE_TAG . '_' . 5,
7171
Product::CACHE_TAG . '_' . 100500,
7272
];
73-
$this->product->expects($this->once())
73+
$this->product->expects($this->exactly(2))
7474
->method('getEntityId')
7575
->willReturn($id);
76-
$this->product->expects($this->once())
76+
$this->product->expects($this->exactly(2))
7777
->method('getTypeId')
7878
->willReturn(Type::TYPE_CODE);
7979
$this->type->expects($this->once())
@@ -82,5 +82,12 @@ public function testAfterGetIdentities()
8282
->willReturn($parentIds);
8383
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities);
8484
$this->assertEquals($expectedIdentities, $identities);
85+
86+
$this->type->expects($this->never())
87+
->method('getParentIdsByChild')
88+
->with($id)
89+
->willReturn($parentIds);
90+
$identities = $this->plugin->afterGetIdentities($this->product, $baseIdentities);
91+
$this->assertEquals($expectedIdentities, $identities);
8592
}
8693
}

0 commit comments

Comments
 (0)