Skip to content

Commit

Permalink
magento#2496: Cache repetitive database queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ishakhsuvarov committed Nov 7, 2019
1 parent da9b467 commit 2d38447
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 7 deletions.
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];
}
}
2 changes: 1 addition & 1 deletion Inventory/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<preference for="Magento\InventoryApi\Api\Data\SourceInterface" type="Magento\Inventory\Model\Source"/>
<preference for="Magento\InventoryApi\Api\Data\SourceCarrierLinkInterface" type="Magento\Inventory\Model\SourceCarrierLink"/>
<preference for="Magento\InventoryApi\Api\Data\SourceSearchResultsInterface" type="Magento\Inventory\Model\SourceSearchResults"/>
<preference for="Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface" type="Magento\Inventory\Model\Source\Command\GetSourcesAssignedToStockOrderedByPriority"/>
<preference for="Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface" type="Magento\Inventory\Model\Source\Command\GetSourcesAssignedToStockOrderedByPriorityCache"/>
<preference for="Magento\InventoryApi\Api\GetSourceItemsBySkuInterface" type="Magento\Inventory\Model\SourceItem\Command\GetSourceItemsBySku"/>
<preference for="Magento\InventoryApi\Model\GetSourceCodesBySkusInterface" type="Magento\Inventory\Model\GetSourceCodesBySkus"/>
<preference for="Magento\InventoryApi\Model\SourceCarrierLinkManagementInterface" type="Magento\Inventory\Model\SourceCarrierLinkManagement"/>
Expand Down
57 changes: 57 additions & 0 deletions InventoryCatalog/Model/GetProductIdsBySkusCache.php
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];
}
}
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];
}
}
4 changes: 2 additions & 2 deletions InventoryCatalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface" type="Magento\InventoryCatalog\Model\DefaultStockProvider"/>
<preference for="Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface" type="Magento\InventoryCatalog\Model\DefaultSourceProvider"/>
<preference for="Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface" type="Magento\InventoryCatalog\Model\GetProductIdsBySkus"/>
<preference for="Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface" type="Magento\InventoryCatalog\Model\ResourceModel\GetProductTypesBySkus" />
<preference for="Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface" type="Magento\InventoryCatalog\Model\GetProductIdsBySkusCache"/>
<preference for="Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface" type="Magento\InventoryCatalog\Model\ResourceModel\GetProductTypesBySkusCache" />
<preference for="Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface" type="Magento\InventoryCatalog\Model\GetSkusByProductIds"/>
<preference for="Magento\InventoryCatalogApi\Model\IsSingleSourceModeInterface" type="Magento\InventoryCatalog\Model\IsSingleSourceMode"/>
<type name="Magento\InventoryApi\Api\StockRepositoryInterface">
Expand Down
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];
}
}
4 changes: 4 additions & 0 deletions InventoryConfiguration/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
<plugin name="allow_negative_min_qty_in_config"
type="Magento\InventoryConfiguration\Plugin\CatalogInventory\Model\System\Config\Backend\Minqty\AllowNegativeMinQtyInConfigPlugin"/>
</type>
<type name="Magento\InventoryConfiguration\Model\GetLegacyStockItem">
<plugin name="cache_legacy_stock_item"
type="Magento\InventoryConfiguration\Plugin\InventoryConfiguration\Model\GetLegacyStockItemCache"/>
</type>
</config>
47 changes: 47 additions & 0 deletions InventoryIndexer/Model/ResourceModel/GetStockItemDataCache.php
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];
}
}
2 changes: 1 addition & 1 deletion InventoryIndexer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<plugin name="invalidate_after_stock_source_links_delete" type="Magento\InventoryIndexer\Plugin\InventoryApi\InvalidateAfterStockSourceLinksDeletePlugin"/>
</type>
<!-- Stock Item -->
<preference for="Magento\InventorySalesApi\Model\GetStockItemDataInterface" type="Magento\InventoryIndexer\Model\ResourceModel\GetStockItemData"/>
<preference for="Magento\InventorySalesApi\Model\GetStockItemDataInterface" type="Magento\InventoryIndexer\Model\ResourceModel\GetStockItemDataCache"/>
<preference for="Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface" type="Magento\InventoryIndexer\Model\StockIndexTableNameResolver"/>
<type name="Magento\InventoryIndexer\Model\StockIndexTableNameResolver">
<arguments>
Expand Down
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];
}
}
2 changes: 1 addition & 1 deletion InventoryReservations/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<preference for="Magento\InventoryReservationsApi\Model\ReservationInterface" type="Magento\InventoryReservations\Model\Reservation"/>
<preference for="Magento\InventoryReservationsApi\Model\ReservationBuilderInterface" type="Magento\InventoryReservations\Model\ReservationBuilder"/>
<preference for="Magento\InventoryReservationsApi\Model\CleanupReservationsInterface" type="Magento\InventoryReservations\Model\ResourceModel\CleanupReservations"/>
<preference for="Magento\InventoryReservationsApi\Model\GetReservationsQuantityInterface" type="Magento\InventoryReservations\Model\ResourceModel\GetReservationsQuantity"/>
<preference for="Magento\InventoryReservationsApi\Model\GetReservationsQuantityInterface" type="Magento\InventoryReservations\Model\ResourceModel\GetReservationsQuantityCache"/>
<type name="Magento\InventoryReservations\Model\ResourceModel\CleanupReservations">
<arguments>
<argument name="groupConcatMaxLen" xsi:type="number">32768</argument>
Expand Down
51 changes: 51 additions & 0 deletions InventorySales/Model/GetAssignedSalesChannelsForStockCache.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\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];
}
}
Loading

0 comments on commit 2d38447

Please sign in to comment.