Skip to content

Commit e10b85f

Browse files
authored
SOL-477 Widget performance optimization (#287)
SOL-477 Widget performance optimization
1 parent 9ab6102 commit e10b85f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Spryker/Client/ProductStorage/Finder/AbstractProductViewTransferFinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ protected function mapProductData(array $productStorageDataCollection, string $l
9999
$productSelectedAttributes = $this->getProductSelectedAttributes($productStorageData, $selectedAttributes);
100100
$productViewTransfer = $this
101101
->productStorageDataMapper
102-
->mapProductStorageData($localeName, $productStorageData, $this->getProductSelectedAttributes($productStorageData, $selectedAttributes));
102+
->mapProductStorageData($localeName, $productStorageData, $productSelectedAttributes);
103103
$this->cacheProductViewTransfer($productViewTransfer, $localeName, $productSelectedAttributes);
104104

105105
$productViewTransfers[$this->getProductId($productViewTransfer)] = $productViewTransfer;

src/Spryker/Client/ProductStorage/Mapper/ProductStorageDataMapper.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class ProductStorageDataMapper implements ProductStorageDataMapperInterface
3636
*/
3737
protected $localeClient;
3838

39+
/**
40+
* @var array<string, \Generated\Shared\Transfer\ProductViewTransfer>
41+
*/
42+
protected static array $mappingCache = [];
43+
3944
/**
4045
* @uses \Spryker\Client\ProductStorage\Mapper\ProductStorageDataMapper::filterProductStorageExpanderPlugins()
4146
*
@@ -67,6 +72,12 @@ public function mapProductStorageData(
6772
array $selectedAttributes = [],
6873
?ProductStorageCriteriaTransfer $productStorageCriteriaTransfer = null
6974
) {
75+
$cacheKey = $this->generateCacheKey($locale, $productStorageData, $selectedAttributes, $productStorageCriteriaTransfer);
76+
77+
if (isset(static::$mappingCache[$cacheKey])) {
78+
return static::$mappingCache[$cacheKey];
79+
}
80+
7081
$productStorageData = $this->filterAbstractProductVariantsData($productStorageData);
7182
$productViewTransfer = $this->createProductViewTransfer($productStorageData);
7283
$productViewTransfer->setSelectedAttributes($selectedAttributes);
@@ -81,9 +92,29 @@ public function mapProductStorageData(
8192
$productViewTransfer = $productViewExpanderPlugin->expandProductViewTransfer($productViewTransfer, $productStorageData, $locale);
8293
}
8394

95+
static::$mappingCache[$cacheKey] = $productViewTransfer;
96+
8497
return $productViewTransfer;
8598
}
8699

100+
protected function generateCacheKey(
101+
string $locale,
102+
array $productStorageData,
103+
array $selectedAttributes,
104+
?ProductStorageCriteriaTransfer $productStorageCriteriaTransfer
105+
): string {
106+
$criteriaKey = $productStorageCriteriaTransfer !== null
107+
? serialize($productStorageCriteriaTransfer->toArray())
108+
: 'null';
109+
110+
return md5(
111+
$locale
112+
. serialize($productStorageData)
113+
. serialize($selectedAttributes)
114+
. $criteriaKey,
115+
);
116+
}
117+
87118
protected function filterAbstractProductVariantsData(array $productStorageData): array
88119
{
89120
return $this->productAbstractVariantsRestrictionFilter->filterAbstractProductVariantsData($productStorageData);

0 commit comments

Comments
 (0)