@@ -232,7 +232,8 @@ public function __construct(
232232 public function get ($ sku , $ editMode = false , $ storeId = null , $ forceReload = false )
233233 {
234234 $ cacheKey = $ this ->getCacheKey ([$ editMode , $ storeId ]);
235- if (!isset ($ this ->instances [$ sku ][$ cacheKey ]) || $ forceReload ) {
235+ $ cachedProduct = $ this ->getProductFromLocalCache ($ sku , $ cacheKey );
236+ if ($ cachedProduct === null || $ forceReload ) {
236237 $ product = $ this ->productFactory ->create ();
237238
238239 $ productId = $ this ->resourceModel ->getIdBySku ($ sku );
@@ -247,11 +248,9 @@ public function get($sku, $editMode = false, $storeId = null, $forceReload = fal
247248 }
248249 $ product ->load ($ productId );
249250 $ this ->cacheProduct ($ cacheKey , $ product );
251+ $ cachedProduct = $ product ;
250252 }
251- if (!isset ($ this ->instances [$ sku ])) {
252- $ sku = trim ($ sku );
253- }
254- return $ this ->instances [$ sku ][$ cacheKey ];
253+ return $ cachedProduct ;
255254 }
256255
257256 /**
@@ -307,7 +306,7 @@ protected function getCacheKey($data)
307306 private function cacheProduct ($ cacheKey , \Magento \Catalog \Api \Data \ProductInterface $ product )
308307 {
309308 $ this ->instancesById [$ product ->getId ()][$ cacheKey ] = $ product ;
310- $ this ->instances [ $ product -> getSku ()][ $ cacheKey ] = $ product ;
309+ $ this ->saveProductInLocalCache ( $ product , $ cacheKey ) ;
311310
312311 if ($ this ->cacheLimit && count ($ this ->instances ) > $ this ->cacheLimit ) {
313312 $ offset = round ($ this ->cacheLimit / -2 );
@@ -340,7 +339,7 @@ protected function initializeProductData(array $productData, $createNew)
340339 unset($ this ->instancesById [$ productData ['id ' ]]);
341340 $ product = $ this ->getById ($ productData ['id ' ]);
342341 } else {
343- unset( $ this ->instances [ $ productData ['sku ' ] ]);
342+ $ this ->removeProductFromLocalCache ( $ productData ['sku ' ]);
344343 $ product = $ this ->get ($ productData ['sku ' ]);
345344 }
346345 }
@@ -495,7 +494,7 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
495494 if ($ tierPrices !== null ) {
496495 $ product ->setData ('tier_price ' , $ tierPrices );
497496 }
498- unset( $ this ->instances [ $ product ->getSku ()] );
497+ $ this ->removeProductFromLocalCache ( $ product ->getSku ());
499498 unset($ this ->instancesById [$ product ->getId ()]);
500499 $ this ->resourceModel ->save ($ product );
501500 } catch (ConnectionException $ exception ) {
@@ -529,7 +528,7 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
529528 } catch (\Exception $ e ) {
530529 throw new \Magento \Framework \Exception \CouldNotSaveException (__ ('Unable to save product ' ), $ e );
531530 }
532- unset( $ this ->instances [ $ product ->getSku ()] );
531+ $ this ->removeProductFromLocalCache ( $ product ->getSku ());
533532 unset($ this ->instancesById [$ product ->getId ()]);
534533 return $ this ->get ($ product ->getSku (), false , $ product ->getStoreId ());
535534 }
@@ -542,7 +541,7 @@ public function delete(\Magento\Catalog\Api\Data\ProductInterface $product)
542541 $ sku = $ product ->getSku ();
543542 $ productId = $ product ->getId ();
544543 try {
545- unset( $ this ->instances [ $ product ->getSku ()] );
544+ $ this ->removeProductFromLocalCache ( $ product ->getSku ());
546545 unset($ this ->instancesById [$ product ->getId ()]);
547546 $ this ->resourceModel ->delete ($ product );
548547 } catch (ValidatorException $ e ) {
@@ -552,7 +551,7 @@ public function delete(\Magento\Catalog\Api\Data\ProductInterface $product)
552551 __ ('Unable to remove product %1 ' , $ sku )
553552 );
554553 }
555- unset( $ this ->instances [ $ sku] );
554+ $ this ->removeProductFromLocalCache ( $ sku );
556555 unset($ this ->instancesById [$ productId ]);
557556 return true ;
558557 }
@@ -675,4 +674,56 @@ private function getCollectionProcessor()
675674 }
676675 return $ this ->collectionProcessor ;
677676 }
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+ }
678729}
0 commit comments