forked from magento/inventory
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
magento#2496: Cache repetitive database queries
- Loading branch information
1 parent
da9b467
commit 2d38447
Showing
14 changed files
with
408 additions
and
7 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
Inventory/Model/Source/Command/GetSourcesAssignedToStockOrderedByPriorityCache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Inventory\Model\Source\Command; | ||
|
||
use Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class GetSourcesAssignedToStockOrderedByPriorityCache implements GetSourcesAssignedToStockOrderedByPriorityInterface | ||
{ | ||
/** | ||
* @var GetSourcesAssignedToStockOrderedByPriority | ||
*/ | ||
private $getSourcesAssignedToStock; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $sourcesAssignedToStock = []; | ||
|
||
/** | ||
* @param GetSourcesAssignedToStockOrderedByPriority $getSourcesAssignedToStockOrderedByPriority | ||
*/ | ||
public function __construct( | ||
GetSourcesAssignedToStockOrderedByPriority $getSourcesAssignedToStockOrderedByPriority | ||
) { | ||
$this->getSourcesAssignedToStock = $getSourcesAssignedToStockOrderedByPriority; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(int $stockId): array | ||
{ | ||
if (!isset($this->sourcesAssignedToStock[$stockId])) { | ||
$this->sourcesAssignedToStock[$stockId] = $this->getSourcesAssignedToStock->execute($stockId); | ||
} | ||
|
||
return $this->sourcesAssignedToStock[$stockId]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryCatalog\Model; | ||
|
||
use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class GetProductIdsBySkusCache implements GetProductIdsBySkusInterface | ||
{ | ||
/** | ||
* @var GetProductIdsBySkus | ||
*/ | ||
private $getProductIdsBySkus; | ||
|
||
/** | ||
* @var Json | ||
*/ | ||
private $jsonSerializer; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $productIdsBySkus = []; | ||
|
||
/** | ||
* @param GetProductIdsBySkus $getProductIdsBySkus | ||
* @param Json $jsonSerializer | ||
*/ | ||
public function __construct( | ||
GetProductIdsBySkus $getProductIdsBySkus, | ||
Json $jsonSerializer | ||
) { | ||
$this->getProductIdsBySkus = $getProductIdsBySkus; | ||
$this->jsonSerializer = $jsonSerializer; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(array $skus): array | ||
{ | ||
$cacheKey = $this->jsonSerializer->serialize($skus); | ||
if (!isset($this->productIdsBySkus[$cacheKey])) { | ||
$this->productIdsBySkus[$cacheKey] = $this->getProductIdsBySkus->execute($skus); | ||
} | ||
|
||
return $this->productIdsBySkus[$cacheKey]; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
InventoryCatalog/Model/ResourceModel/GetProductTypesBySkusCache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryCatalog\Model\ResourceModel; | ||
|
||
use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface; | ||
use Magento\Framework\Serialize\Serializer\Json; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class GetProductTypesBySkusCache implements GetProductTypesBySkusInterface | ||
{ | ||
/** | ||
* @var GetProductTypesBySkus | ||
*/ | ||
private $getProductTypesBySkus; | ||
|
||
/** | ||
* @var Json | ||
*/ | ||
private $jsonSerializer; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $productTypesBySkus = []; | ||
|
||
/** | ||
* @param GetProductTypesBySkus $getProductTypesBySkus | ||
* @param Json $jsonSerializer | ||
*/ | ||
public function __construct( | ||
GetProductTypesBySkus $getProductTypesBySkus, | ||
Json $jsonSerializer | ||
) { | ||
$this->getProductTypesBySkus = $getProductTypesBySkus; | ||
$this->jsonSerializer = $jsonSerializer; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(array $skus): array | ||
{ | ||
$cacheKey = $this->jsonSerializer->serialize($skus); | ||
if (!isset($this->productTypesBySkus[$cacheKey])) { | ||
$this->productTypesBySkus[$cacheKey] = $this->getProductTypesBySkus->execute($skus); | ||
} | ||
|
||
return $this->productTypesBySkus[$cacheKey]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
InventoryConfiguration/Plugin/InventoryConfiguration/Model/GetLegacyStockItemCache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryConfiguration\Plugin\InventoryConfiguration\Model; | ||
|
||
use Magento\CatalogInventory\Api\Data\StockItemInterface; | ||
use Magento\InventoryConfiguration\Model\GetLegacyStockItem; | ||
|
||
/** | ||
* Caching plugin for GetLegacyStockItem service. | ||
*/ | ||
class GetLegacyStockItemCache | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $legacyStockItemsBySku = []; | ||
|
||
/** | ||
* Cache the result of service to avoid duplicate queries to the database. | ||
* | ||
* @param GetLegacyStockItem $subject | ||
* @param callable $proceed | ||
* @param string $sku | ||
* @return StockItemInterface | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function aroundExecute(GetLegacyStockItem $subject, callable $proceed, string $sku): StockItemInterface | ||
{ | ||
if (!isset($this->legacyStockItemsBySku[$sku])) { | ||
$this->legacyStockItemsBySku[$sku] = $proceed($sku); | ||
} | ||
|
||
return $this->legacyStockItemsBySku[$sku]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
InventoryIndexer/Model/ResourceModel/GetStockItemDataCache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryIndexer\Model\ResourceModel; | ||
|
||
use Magento\InventorySalesApi\Model\GetStockItemDataInterface; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class GetStockItemDataCache implements GetStockItemDataInterface | ||
{ | ||
/** | ||
* @var GetStockItemData | ||
*/ | ||
private $getStockItemData; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $stockItemData = [[]]; | ||
|
||
/** | ||
* @param GetStockItemData $getStockItemData | ||
*/ | ||
public function __construct( | ||
GetStockItemData $getStockItemData | ||
) { | ||
$this->getStockItemData = $getStockItemData; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(string $sku, int $stockId): ?array | ||
{ | ||
if (!isset($this->stockItemData[$stockId][$sku])) { | ||
$this->stockItemData[$stockId][$sku] = $this->getStockItemData->execute($sku, $stockId); | ||
} | ||
|
||
return $this->stockItemData[$stockId][$sku]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
InventoryReservations/Model/ResourceModel/GetReservationsQuantityCache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\InventoryReservations\Model\ResourceModel; | ||
|
||
use Magento\InventoryReservationsApi\Model\GetReservationsQuantityInterface; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class GetReservationsQuantityCache implements GetReservationsQuantityInterface | ||
{ | ||
/** | ||
* @var GetReservationsQuantity | ||
*/ | ||
private $getReservationsQuantity; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $reservationsQuantity = [[]]; | ||
|
||
/** | ||
* @param GetReservationsQuantity $getReservationsQuantity | ||
*/ | ||
public function __construct( | ||
GetReservationsQuantity $getReservationsQuantity | ||
) { | ||
$this->getReservationsQuantity = $getReservationsQuantity; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(string $sku, int $stockId): float | ||
{ | ||
if (!isset($this->reservationsQuantity[$sku][$stockId])) { | ||
$this->reservationsQuantity[$sku][$stockId] = $this->getReservationsQuantity->execute($sku, $stockId); | ||
} | ||
|
||
return $this->reservationsQuantity[$sku][$stockId]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
InventorySales/Model/GetAssignedSalesChannelsForStockCache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\InventorySales\Model; | ||
|
||
use Magento\InventorySalesApi\Model\GetAssignedSalesChannelsForStockInterface; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class GetAssignedSalesChannelsForStockCache implements GetAssignedSalesChannelsForStockInterface | ||
{ | ||
/** | ||
* @var GetAssignedSalesChannelsForStock | ||
*/ | ||
private $getAssignedSalesChannelsForStock; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $salesChannelsAssignedToStocks = []; | ||
|
||
/** | ||
* @param GetAssignedSalesChannelsForStock $getAssignedSalesChannelsForStock | ||
*/ | ||
public function __construct( | ||
GetAssignedSalesChannelsForStock $getAssignedSalesChannelsForStock | ||
) { | ||
$this->getAssignedSalesChannelsForStock = $getAssignedSalesChannelsForStock; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(int $stockId): array | ||
{ | ||
if (!isset($this->salesChannelsAssignedToStocks[$stockId])) { | ||
$assignedSalesChannels = $this->getAssignedSalesChannelsForStock->execute($stockId); | ||
if (empty($assignedSalesChannels)) { | ||
return $assignedSalesChannels; | ||
} | ||
$this->salesChannelsAssignedToStocks[$stockId] = $assignedSalesChannels; | ||
} | ||
|
||
return $this->salesChannelsAssignedToStocks[$stockId]; | ||
} | ||
} |
Oops, something went wrong.