Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache IsProductAssignedToStock service to improve cart performance #2692

Merged
merged 5 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Inventory\Plugin\Inventory\Model\ResourceModel;

use Magento\Inventory\Model\ResourceModel\IsProductAssignedToStock;

/**
* Caching plugin for IsProductAssignedToStock service.
*/
class IsProductAssignedToStockCache
{
/**
* @var array
*/
private $skuToStockIdAssignment = [];

/**
* Cache service result to avoid multiple database calls for same item
*
* @param IsProductAssignedToStock $subject
* @param callable $proceed
* @param string $sku
* @param int $stockId
* @return bool
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundExecute(IsProductAssignedToStock $subject, callable $proceed, string $sku, int $stockId): bool
{
if (!isset($this->skuToStockIdAssignment[$sku][$stockId])) {
$this->skuToStockIdAssignment[$sku][$stockId] = $proceed($sku, $stockId);
}
return $this->skuToStockIdAssignment[$sku][$stockId];
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Inventory/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,7 @@
</arguments>
</type>
<preference for="Magento\InventoryApi\Model\IsProductAssignedToStockInterface" type="Magento\Inventory\Model\ResourceModel\IsProductAssignedToStock"/>
<type name="Magento\Inventory\Model\ResourceModel\IsProductAssignedToStock">
<plugin name="cache_product_stock_assignment_check" type="Magento\Inventory\Plugin\Inventory\Model\ResourceModel\IsProductAssignedToStockCache"/>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<type name="Magento\InventoryConfiguration\Model\GetLegacyStockItem">
<plugin name="cache_legacy_stock_item" disabled="true"/>
</type>
<type name="Magento\Inventory\Model\ResourceModel\IsProductAssignedToStock">
<plugin name="cache_product_stock_assignment_check" disabled="true"/>
</type>
<preference for="Magento\InventoryApi\Api\GetSourcesAssignedToStockOrderedByPriorityInterface" type="Magento\Inventory\Model\Source\Command\GetSourcesAssignedToStockOrderedByPriority"/>
<preference for="Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface" type="Magento\InventoryCatalog\Model\GetProductIdsBySkus"/>
<preference for="Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface" type="Magento\InventoryCatalog\Model\ResourceModel\GetProductTypesBySkus" />
Expand Down