Skip to content

Commit

Permalink
Merge pull request #179 from magento-gl/GL_Mainline_PR_19012022_V2
Browse files Browse the repository at this point in the history
Gl mainline pr 19012022 v2
  • Loading branch information
sidolov authored Jan 21, 2022
2 parents 041ca0e + 1883e70 commit dc3fc54
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 9 deletions.
13 changes: 12 additions & 1 deletion InventoryCatalog/Observer/PreloadCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogInventory\Model\Stock;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\InventoryCatalog\Model\Cache\ProductIdsBySkusStorage;
use Magento\InventoryCatalog\Model\Cache\ProductSkusByIdsStorage;
use Magento\InventoryCatalog\Model\Cache\ProductTypesBySkusStorage;
use Magento\InventoryCatalog\Model\LegacyStockStatusCache;
use Magento\InventoryIndexer\Model\GetStockItemData\CacheStorage as StockItemDataCacheStorage;

/**
* Preload id/sku, sku/id and sku/type pairs into cache
Expand All @@ -41,22 +43,30 @@ class PreloadCache implements ObserverInterface
*/
private $legacyStockStatusCache;

/**
* @var StockItemDataCacheStorage
*/
private $stockItemDataCacheStorage;

/**
* @param ProductTypesBySkusStorage $productTypesBySkusStorage
* @param ProductIdsBySkusStorage $productIdsBySkusStorage
* @param ProductSkusByIdsStorage $productSkusByIdsStorage
* @param LegacyStockStatusCache $legacyStockStatusCache
* @param StockItemDataCacheStorage $stockItemDataCacheStorage
*/
public function __construct(
ProductTypesBySkusStorage $productTypesBySkusStorage,
ProductIdsBySkusStorage $productIdsBySkusStorage,
ProductSkusByIdsStorage $productSkusByIdsStorage,
LegacyStockStatusCache $legacyStockStatusCache
LegacyStockStatusCache $legacyStockStatusCache,
StockItemDataCacheStorage $stockItemDataCacheStorage
) {
$this->productTypesBySkusStorage = $productTypesBySkusStorage;
$this->productIdsBySkusStorage = $productIdsBySkusStorage;
$this->productSkusByIdsStorage = $productSkusByIdsStorage;
$this->legacyStockStatusCache = $legacyStockStatusCache;
$this->stockItemDataCacheStorage = $stockItemDataCacheStorage;
}

/**
Expand All @@ -71,6 +81,7 @@ public function execute(Observer $observer)
$this->productTypesBySkusStorage->set((string) $product->getSku(), (string) $product->getTypeId());
$this->productIdsBySkusStorage->set((string) $product->getSku(), (int) $product->getId());
$this->productSkusByIdsStorage->set((int) $product->getId(), (string) $product->getSku());
$this->stockItemDataCacheStorage->delete(Stock::DEFAULT_STOCK_ID, $product->getSku());
}
$productIds = array_keys($productCollection->getItems());
if ($productIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Model\AbstractModel;
use Magento\InventoryCatalog\Model\Cache\ProductIdsBySkusStorage;
use Magento\InventoryCatalog\Model\Cache\ProductSkusByIdsStorage;
use Magento\InventoryCatalog\Model\GetDefaultSourceItemBySku;
use Magento\InventoryCatalog\Model\ResourceModel\SetDataToLegacyStockStatus;
use Magento\InventoryCatalog\Model\UpdateSourceItemBasedOnLegacyStockItem;
use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface;
use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface;
use Magento\InventoryCatalog\Model\UpdateSourceItemBasedOnLegacyStockItem;
use Magento\InventoryConfiguration\Model\LegacyStockItem\CacheStorage;
use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface;
use Magento\InventorySalesApi\Api\AreProductsSalableInterface;

Expand Down Expand Up @@ -69,6 +72,21 @@ class UpdateSourceItemAtLegacyStockItemSavePlugin
*/
private $setDataToLegacyStockStatus;

/**
* @var ProductIdsBySkusStorage
*/
private $productIdsBySkusStorage;

/**
* @var ProductSkusByIdsStorage
*/
private $productSkusByIdsStorage;

/**
* @var CacheStorage
*/
private $itemCacheStorage;

/**
* @param UpdateSourceItemBasedOnLegacyStockItem $updateSourceItemBasedOnLegacyStockItem
* @param ResourceConnection $resourceConnection
Expand All @@ -78,6 +96,10 @@ class UpdateSourceItemAtLegacyStockItemSavePlugin
* @param GetDefaultSourceItemBySku $getDefaultSourceItemBySku
* @param AreProductsSalableInterface $areProductsSalable
* @param SetDataToLegacyStockStatus $setDataToLegacyStockStatus
* @param ProductIdsBySkusStorage $productIdsBySkusStorage
* @param ProductSkusByIdsStorage $productSkusByIdsStorage
* @param CacheStorage $itemCacheStorage
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
UpdateSourceItemBasedOnLegacyStockItem $updateSourceItemBasedOnLegacyStockItem,
Expand All @@ -87,7 +109,10 @@ public function __construct(
GetSkusByProductIdsInterface $getSkusByProductIds,
GetDefaultSourceItemBySku $getDefaultSourceItemBySku,
AreProductsSalableInterface $areProductsSalable,
SetDataToLegacyStockStatus $setDataToLegacyStockStatus
SetDataToLegacyStockStatus $setDataToLegacyStockStatus,
ProductIdsBySkusStorage $productIdsBySkusStorage,
ProductSkusByIdsStorage $productSkusByIdsStorage,
CacheStorage $itemCacheStorage
) {
$this->updateSourceItemBasedOnLegacyStockItem = $updateSourceItemBasedOnLegacyStockItem;
$this->resourceConnection = $resourceConnection;
Expand All @@ -97,6 +122,9 @@ public function __construct(
$this->getDefaultSourceItemBySku = $getDefaultSourceItemBySku;
$this->areProductsSalable = $areProductsSalable;
$this->setDataToLegacyStockStatus = $setDataToLegacyStockStatus;
$this->productIdsBySkusStorage = $productIdsBySkusStorage;
$this->productSkusByIdsStorage = $productSkusByIdsStorage;
$this->itemCacheStorage = $itemCacheStorage;
}

/**
Expand Down Expand Up @@ -126,6 +154,8 @@ public function aroundSave(ItemResourceModel $subject, callable $proceed, Abstra

$productSku = $this->getSkusByProductIds
->execute([$legacyStockItem->getProductId()])[$legacyStockItem->getProductId()];
$this->updateCaches($legacyStockItem, $productSku);

$stockStatuses = [];
$areSalableResults = $this->areProductsSalable->execute([$productSku], Stock::DEFAULT_STOCK_ID);
foreach ($areSalableResults as $productSalable) {
Expand Down Expand Up @@ -187,4 +217,25 @@ private function getTypeId(Item $legacyStockItem): string

return $typeId;
}

/**
* Update legacy item caches
*
* @param Item $legacyStockItem
* @param string $productSku
* @return void
*/
private function updateCaches(Item $legacyStockItem, string $productSku): void
{
$this->productIdsBySkusStorage->set(
$productSku,
$legacyStockItem->getProductId()
);
$this->productSkusByIdsStorage->set(
(int)$legacyStockItem->getProductId(),
$productSku
);
// Remove cache to get updated legacy stock item on the next request.
$this->itemCacheStorage->delete($productSku);
}
}
51 changes: 51 additions & 0 deletions InventoryIndexer/Model/GetStockItemData/CacheStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryIndexer\Model\GetStockItemData;

class CacheStorage
{
/**
* @var array
*/
private $cachedItemData = [[]];

/**
* Save item to cache
*
* @param int $stockId
* @param string $sku
* @param array $stockItemData
*/
public function set(int $stockId, string $sku, array $stockItemData): void
{
$this->cachedItemData[$stockId][$sku] = $stockItemData;
}

/**
* Get item from cache
*
* @param int $stockId
* @param string $sku
* @return array
*/
public function get(int $stockId, string $sku): ?array
{
return $this->cachedItemData[$stockId][$sku] ?? null;
}

/**
* Delete item from cache
*
* @param int $stockId
* @param string $sku
*/
public function delete(int $stockId, string $sku): void
{
unset($this->cachedItemData[$stockId][$sku]);
}
}
24 changes: 18 additions & 6 deletions InventoryIndexer/Model/ResourceModel/GetStockItemDataCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

namespace Magento\InventoryIndexer\Model\ResourceModel;

use Magento\Framework\App\ObjectManager;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
use Magento\InventoryIndexer\Model\GetStockItemData\CacheStorage;

/**
* @inheritdoc
Expand All @@ -20,28 +22,38 @@ class GetStockItemDataCache implements GetStockItemDataInterface
private $getStockItemData;

/**
* @var array
* @var CacheStorage
*/
private $stockItemData = [[]];
private $cacheStorage;

/**
* @param GetStockItemData $getStockItemData
* @param CacheStorage|null $cacheStorage
*/
public function __construct(
GetStockItemData $getStockItemData
GetStockItemData $getStockItemData,
CacheStorage $cacheStorage = null
) {
$this->getStockItemData = $getStockItemData;
$this->cacheStorage = $cacheStorage ?: ObjectManager::getInstance()
->get(CacheStorage::class);
}

/**
* @inheritdoc
*/
public function execute(string $sku, int $stockId): ?array
{
if (!isset($this->stockItemData[$stockId][$sku])) {
$this->stockItemData[$stockId][$sku] = $this->getStockItemData->execute($sku, $stockId);
if ($this->cacheStorage->get($stockId, $sku)) {
return $this->cacheStorage->get($stockId, $sku);
}
/** @var array $stockItemData */
$stockItemData = $this->getStockItemData->execute($sku, $stockId);
/* Add to cache a new item */
if (!empty($stockItemData)) {
$this->cacheStorage->set($stockId, $sku, $stockItemData);
}

return $this->stockItemData[$stockId][$sku];
return $stockItemData;
}
}

0 comments on commit dc3fc54

Please sign in to comment.