@@ -232,7 +232,8 @@ public function __construct(
232
232
public function get ($ sku , $ editMode = false , $ storeId = null , $ forceReload = false )
233
233
{
234
234
$ cacheKey = $ this ->getCacheKey ([$ editMode , $ storeId ]);
235
- if (!isset ($ this ->instances [$ sku ][$ cacheKey ]) || $ forceReload ) {
235
+ $ cachedProduct = $ this ->getProductFromLocalCache ($ sku , $ cacheKey );
236
+ if ($ cachedProduct === null || $ forceReload ) {
236
237
$ product = $ this ->productFactory ->create ();
237
238
238
239
$ productId = $ this ->resourceModel ->getIdBySku ($ sku );
@@ -247,11 +248,9 @@ public function get($sku, $editMode = false, $storeId = null, $forceReload = fal
247
248
}
248
249
$ product ->load ($ productId );
249
250
$ this ->cacheProduct ($ cacheKey , $ product );
251
+ $ cachedProduct = $ product ;
250
252
}
251
- if (!isset ($ this ->instances [$ sku ])) {
252
- $ sku = trim ($ sku );
253
- }
254
- return $ this ->instances [$ sku ][$ cacheKey ];
253
+ return $ cachedProduct ;
255
254
}
256
255
257
256
/**
@@ -307,7 +306,7 @@ protected function getCacheKey($data)
307
306
private function cacheProduct ($ cacheKey , \Magento \Catalog \Api \Data \ProductInterface $ product )
308
307
{
309
308
$ this ->instancesById [$ product ->getId ()][$ cacheKey ] = $ product ;
310
- $ this ->instances [ $ product -> getSku ()][ $ cacheKey ] = $ product ;
309
+ $ this ->saveProductInLocalCache ( $ product , $ cacheKey ) ;
311
310
312
311
if ($ this ->cacheLimit && count ($ this ->instances ) > $ this ->cacheLimit ) {
313
312
$ offset = round ($ this ->cacheLimit / -2 );
@@ -340,7 +339,7 @@ protected function initializeProductData(array $productData, $createNew)
340
339
unset($ this ->instancesById [$ productData ['id ' ]]);
341
340
$ product = $ this ->getById ($ productData ['id ' ]);
342
341
} else {
343
- unset( $ this ->instances [ $ productData ['sku ' ] ]);
342
+ $ this ->removeProductFromLocalCache ( $ productData ['sku ' ]);
344
343
$ product = $ this ->get ($ productData ['sku ' ]);
345
344
}
346
345
}
@@ -495,7 +494,7 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
495
494
if ($ tierPrices !== null ) {
496
495
$ product ->setData ('tier_price ' , $ tierPrices );
497
496
}
498
- unset( $ this ->instances [ $ product ->getSku ()] );
497
+ $ this ->removeProductFromLocalCache ( $ product ->getSku ());
499
498
unset($ this ->instancesById [$ product ->getId ()]);
500
499
$ this ->resourceModel ->save ($ product );
501
500
} catch (ConnectionException $ exception ) {
@@ -529,7 +528,7 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
529
528
} catch (\Exception $ e ) {
530
529
throw new \Magento \Framework \Exception \CouldNotSaveException (__ ('Unable to save product ' ), $ e );
531
530
}
532
- unset( $ this ->instances [ $ product ->getSku ()] );
531
+ $ this ->removeProductFromLocalCache ( $ product ->getSku ());
533
532
unset($ this ->instancesById [$ product ->getId ()]);
534
533
return $ this ->get ($ product ->getSku (), false , $ product ->getStoreId ());
535
534
}
@@ -542,7 +541,7 @@ public function delete(\Magento\Catalog\Api\Data\ProductInterface $product)
542
541
$ sku = $ product ->getSku ();
543
542
$ productId = $ product ->getId ();
544
543
try {
545
- unset( $ this ->instances [ $ product ->getSku ()] );
544
+ $ this ->removeProductFromLocalCache ( $ product ->getSku ());
546
545
unset($ this ->instancesById [$ product ->getId ()]);
547
546
$ this ->resourceModel ->delete ($ product );
548
547
} catch (ValidatorException $ e ) {
@@ -552,7 +551,7 @@ public function delete(\Magento\Catalog\Api\Data\ProductInterface $product)
552
551
__ ('Unable to remove product %1 ' , $ sku )
553
552
);
554
553
}
555
- unset( $ this ->instances [ $ sku] );
554
+ $ this ->removeProductFromLocalCache ( $ sku );
556
555
unset($ this ->instancesById [$ productId ]);
557
556
return true ;
558
557
}
@@ -675,4 +674,56 @@ private function getCollectionProcessor()
675
674
}
676
675
return $ this ->collectionProcessor ;
677
676
}
677
+
678
+ /**
679
+ * Gets product from the local cache by SKU.
680
+ *
681
+ * @param string $sku
682
+ * @param string $cacheKey
683
+ * @return Product|null
684
+ */
685
+ private function getProductFromLocalCache (string $ sku , string $ cacheKey )
686
+ {
687
+ $ preparedSku = $ this ->prepareSku ($ sku );
688
+ if (!isset ($ this ->instances [$ preparedSku ])) {
689
+ return null ;
690
+ }
691
+
692
+ return $ this ->instances [$ preparedSku ][$ cacheKey ] ?? null ;
693
+ }
694
+
695
+ /**
696
+ * Removes product in the local cache.
697
+ *
698
+ * @param string $sku
699
+ * @return void
700
+ */
701
+ private function removeProductFromLocalCache (string $ sku )
702
+ {
703
+ $ preparedSku = $ this ->prepareSku ($ sku );
704
+ unset($ this ->instances [$ preparedSku ]);
705
+ }
706
+
707
+ /**
708
+ * Saves product in the local cache.
709
+ *
710
+ * @param Product $product
711
+ * @param string $cacheKey
712
+ */
713
+ private function saveProductInLocalCache (Product $ product , string $ cacheKey )
714
+ {
715
+ $ preparedSku = $ this ->prepareSku ($ product ->getSku ());
716
+ $ this ->instances [$ preparedSku ][$ cacheKey ] = $ product ;
717
+ }
718
+
719
+ /**
720
+ * Converts SKU to lower case and trims.
721
+ *
722
+ * @param string $sku
723
+ * @return string
724
+ */
725
+ private function prepareSku (string $ sku ): string
726
+ {
727
+ return mb_strtolower (trim ($ sku ));
728
+ }
678
729
}
0 commit comments