-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hotfix for large number of queries in scenarios related to Shopping Cart
- Loading branch information
1 parent
ba2c699
commit ad57569
Showing
17 changed files
with
454 additions
and
7 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...agento/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
57 changes: 57 additions & 0 deletions
57
app/code/Magento/InventoryCatalog/Model/GetProductIdsBySkusCache.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; | ||
|
||
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
app/code/Magento/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
...to/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
app/code/Magento/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
app/code/Magento/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
47 changes: 47 additions & 0 deletions
47
app/code/Magento/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,47 @@ | ||
<?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])) { | ||
$this->salesChannelsAssignedToStocks[$stockId] = $this->getAssignedSalesChannelsForStock->execute($stockId); | ||
} | ||
|
||
return $this->salesChannelsAssignedToStocks[$stockId]; | ||
} | ||
} |
Oops, something went wrong.
ad57569
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disclaimer: This patch does wonder for performance, great work!
This hotfix causes some problems during the initial magento installation process when adding/modifying websites (in my case, we were adding one additional website and then changing the code of the default website) which gives a nondescript "Validation failed" message.
Stack trace
This is where the message originates from (but this is only a symptom of another problem):
inventory/app/code/Magento/InventorySales/Model/Stock/Validator/SalesChannelsValidator.php
Lines 66 to 72 in ad57569
The rabbit hole goes deep, but what happens is that the cache will cache an empty response (before the default website is even installed), so Magento will think that the base website is not associated with the default stock for the entirety of the installation process (unless working from an already working database, so this mainly impacts CI users).
More specifically, this function now believes that there is no stock attached (despite the
inventory_stock_sales_channel
table being correct):inventory/app/code/Magento/InventorySales/Plugin/Store/Model/ResourceModel/Website/AssignWebsiteToDefaultStockPlugin.php
Lines 87 to 90 in ad57569
This results in the sales channel link between the
base
website and the default stock to disappear.I've devised the below patch which invalidates the cache following an update to the relation between sales channels and their stock.
ad57569
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for this work @k4emic.
I'll work to get your solution included in the patch as well. We still haven't decided if this is something that we would include in the codebase at this point, but if we do, now it is 1 step closer :)