Skip to content

Commit 50c513f

Browse files
authored
Merge pull request #8648 from magento-lynx/MCLOUD-10884-Q4-API-performance-optimizations
[LYNX] Performance improvements
2 parents f8474ca + 5aed474 commit 50c513f

File tree

7 files changed

+243
-62
lines changed

7 files changed

+243
-62
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\Customer\Model\Cache;
18+
19+
class GroupExcludedWebsiteCache
20+
{
21+
/**
22+
* @var array
23+
*/
24+
private array $customerGroupExcludedWebsite = [];
25+
26+
/**
27+
* Adds entry to GroupExcludedWebsite cache
28+
*
29+
* @param int $customerGroupId
30+
* @param array $value
31+
*/
32+
public function addToCache(int $customerGroupId, array $value)
33+
{
34+
$this->customerGroupExcludedWebsite[$customerGroupId] = $value;
35+
}
36+
37+
/**
38+
* Gets entry from GroupExcludedWebsite cache
39+
*
40+
* @param int $customerGroupId
41+
* @return array
42+
*/
43+
public function getFromCache(int $customerGroupId): array
44+
{
45+
return $this->customerGroupExcludedWebsite[$customerGroupId] ?? [];
46+
}
47+
48+
/**
49+
* Checks presence of cached customer group in GroupExcludedWebsite cache
50+
*
51+
* @param int $customerGroupId
52+
* @return bool
53+
*/
54+
public function isCached(int $customerGroupId): bool
55+
{
56+
return isset($this->customerGroupExcludedWebsite[$customerGroupId]);
57+
}
58+
59+
/**
60+
* Cleans the cache
61+
*/
62+
public function invalidate()
63+
{
64+
$this->customerGroupExcludedWebsite = [];
65+
}
66+
}

app/code/Magento/Customer/Model/Plugin/SaveCustomerGroupExcludedWebsite.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,4 @@ private function isValueChanged(array $currentValues, array $newValues): bool
145145
return !($currentValues === array_intersect($currentValues, $newValues)
146146
&& $newValues === array_intersect($newValues, $currentValues));
147147
}
148-
149148
}

app/code/Magento/Customer/Model/ResourceModel/GroupExcludedWebsite.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,40 @@
99

1010
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\Model\ResourceModel\Db\VersionControl\AbstractDb;
12+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
13+
use Magento\Customer\Model\Cache\GroupExcludedWebsiteCache;
14+
use Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite;
15+
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
16+
use Magento\Framework\Model\ResourceModel\Db\Context;
1217

1318
/**
1419
* Excluded customer group website resource model.
1520
*/
16-
class GroupExcludedWebsite extends AbstractDb
21+
class GroupExcludedWebsite extends AbstractDb implements ResetAfterRequestInterface
1722
{
23+
/**
24+
* @var GroupExcludedWebsiteCache $groupExcludedWebsiteCache
25+
*/
26+
private GroupExcludedWebsiteCache $groupExcludedWebsiteCache;
27+
28+
/**
29+
* @param Context $context
30+
* @param Snapshot $entitySnapshot
31+
* @param RelationComposite $entityRelationComposite
32+
* @param GroupExcludedWebsiteCache $groupExcludedWebsiteCache
33+
* @param string $connectionName
34+
*/
35+
public function __construct(
36+
Context $context,
37+
Snapshot $entitySnapshot,
38+
RelationComposite $entityRelationComposite,
39+
GroupExcludedWebsiteCache $groupExcludedWebsiteCache,
40+
$connectionName = null
41+
) {
42+
parent::__construct($context, $entitySnapshot, $entityRelationComposite, $connectionName);
43+
$this->groupExcludedWebsiteCache = $groupExcludedWebsiteCache;
44+
}
45+
1846
/**
1947
* Resource initialization
2048
*
@@ -25,15 +53,36 @@ protected function _construct()
2553
$this->_init('customer_group_excluded_website', 'entity_id');
2654
}
2755

56+
/**
57+
* @inheritDoc
58+
*/
59+
public function _resetState(): void
60+
{
61+
$this->groupExcludedWebsiteCache->invalidate();
62+
}
63+
64+
/**
65+
* Makes sure ExcludedWebsiteCache is invalidated when excluded websites are modified
66+
*/
67+
public function invalidateCache()
68+
{
69+
$this->_resetState();
70+
}
71+
2872
/**
2973
* Retrieve excluded website ids related to customer group.
3074
*
3175
* @param int $customerGroupId
3276
* @return array
3377
* @throws LocalizedException
3478
*/
79+
3580
public function loadCustomerGroupExcludedWebsites(int $customerGroupId): array
3681
{
82+
if ($this->groupExcludedWebsiteCache->isCached($customerGroupId)) {
83+
return $this->groupExcludedWebsiteCache->getFromCache($customerGroupId);
84+
}
85+
3786
$connection = $this->getConnection();
3887
$bind = ['customer_group_id' => $customerGroupId];
3988

@@ -44,7 +93,8 @@ public function loadCustomerGroupExcludedWebsites(int $customerGroupId): array
4493
'customer_group_id = :customer_group_id'
4594
);
4695

47-
return $connection->fetchCol($select, $bind);
96+
$this->groupExcludedWebsiteCache->addToCache($customerGroupId, $connection->fetchCol($select, $bind));
97+
return $this->groupExcludedWebsiteCache->getFromCache($customerGroupId);
4898
}
4999

50100
/**
@@ -76,6 +126,7 @@ public function delete($customerGroupId)
76126
{
77127
$connection = $this->getConnection();
78128
$connection->beginTransaction();
129+
$this->invalidateCache();
79130
try {
80131
$where = $connection->quoteInto('customer_group_id = ?', $customerGroupId);
81132
$connection->delete(
@@ -87,7 +138,6 @@ public function delete($customerGroupId)
87138
$connection->rollBack();
88139
throw $e;
89140
}
90-
91141
return $this;
92142
}
93143

app/code/Magento/Customer/Model/ResourceModel/GroupExcludedWebsiteRepository.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class GroupExcludedWebsiteRepository implements GroupExcludedWebsiteRepositoryIn
2626
* @param GroupExcludedWebsite $groupExcludedWebsiteResourceModel
2727
*/
2828
public function __construct(
29-
GroupExcludedWebsite $groupExcludedWebsiteResourceModel
29+
GroupExcludedWebsite $groupExcludedWebsiteResourceModel,
3030
) {
3131
$this->groupExcludedWebsiteResourceModel = $groupExcludedWebsiteResourceModel;
3232
}
@@ -43,7 +43,6 @@ public function save(GroupExcludedWebsiteInterface $groupExcludedWebsite): Group
4343
__('Could not save customer group website to exclude from customer group: "%1"', $e->getMessage())
4444
);
4545
}
46-
4746
return $groupExcludedWebsite;
4847
}
4948

@@ -78,8 +77,8 @@ public function getAllExcludedWebsites(): array
7877

7978
if (!empty($allExcludedWebsites)) {
8079
foreach ($allExcludedWebsites as $allExcludedWebsite) {
81-
$customerGroupId = (int)$allExcludedWebsite['customer_group_id'];
82-
$websiteId = (int)$allExcludedWebsite['website_id'];
80+
$customerGroupId = (int) $allExcludedWebsite['customer_group_id'];
81+
$websiteId = (int) $allExcludedWebsite['website_id'];
8382
$excludedWebsites[$customerGroupId][] = $websiteId;
8483
}
8584
}
@@ -109,7 +108,7 @@ public function delete(int $customerGroupId): bool
109108
public function deleteByWebsite(int $websiteId): bool
110109
{
111110
try {
112-
return (bool)$this->groupExcludedWebsiteResourceModel->deleteByWebsite($websiteId);
111+
return (bool) $this->groupExcludedWebsiteResourceModel->deleteByWebsite($websiteId);
113112
} catch (LocalizedException $e) {
114113
throw new LocalizedException(
115114
__('Could not delete customer group excluded website by id.')

app/code/Magento/Quote/Model/ResourceModel/Quote/Address/Rate/Collection.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66
namespace Magento\Quote\Model\ResourceModel\Quote\Address\Rate;
77

8+
use Magento\Quote\Model\ResourceModel\Quote\Address\Rate;
9+
810
/**
911
* Quote addresses shipping rates collection
1012
*
11-
* @author Magento Core Team <core@magentocommerce.com>
13+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1214
*/
1315
class Collection extends \Magento\Framework\Model\ResourceModel\Db\VersionControl\Collection
1416
{
@@ -24,15 +26,21 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\VersionContro
2426
*/
2527
private $_carrierFactory;
2628

29+
/**
30+
* @var Delete
31+
*/
32+
private Delete $deleteRates;
33+
2734
/**
2835
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
2936
* @param \Psr\Log\LoggerInterface $logger
3037
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
3138
* @param \Magento\Framework\Event\ManagerInterface $eventManager
3239
* @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot
3340
* @param \Magento\Shipping\Model\CarrierFactoryInterface $carrierFactory
34-
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
35-
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
41+
* @param Delete $deleteRates
42+
* @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
43+
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
3644
*/
3745
public function __construct(
3846
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
@@ -41,8 +49,9 @@ public function __construct(
4149
\Magento\Framework\Event\ManagerInterface $eventManager,
4250
\Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot $entitySnapshot,
4351
\Magento\Shipping\Model\CarrierFactoryInterface $carrierFactory,
52+
Delete $deleteRates,
4453
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
45-
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
54+
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
4655
) {
4756
parent::__construct(
4857
$entityFactory,
@@ -53,6 +62,7 @@ public function __construct(
5362
$connection,
5463
$resource
5564
);
65+
$this->deleteRates = $deleteRates;
5666
$this->_carrierFactory = $carrierFactory;
5767
}
5868

@@ -112,4 +122,27 @@ public function addItem(\Magento\Framework\DataObject $rate)
112122
}
113123
return parent::addItem($rate);
114124
}
125+
126+
/**
127+
* @inheritdoc
128+
*/
129+
public function save()
130+
{
131+
$itemsToDelete = [];
132+
$itemsToSave = [];
133+
/** @var Rate $item */
134+
foreach ($this->getItems() as $item) {
135+
if ($item->isDeleted()) {
136+
$itemsToDelete[] = $item;
137+
} else {
138+
$itemsToSave[] = $item;
139+
}
140+
}
141+
$this->deleteRates->execute($itemsToDelete);
142+
/** @var Rate $item */
143+
foreach ($itemsToSave as $item) {
144+
$item->save();
145+
}
146+
return $this;
147+
}
115148
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2023 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ***********************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Quote\Model\ResourceModel\Quote\Address\Rate;
20+
21+
use Magento\Framework\App\ResourceConnection;
22+
use Magento\Quote\Model\Quote\Address\Rate;
23+
24+
class Delete
25+
{
26+
private const TABLE = 'quote_shipping_rate';
27+
private const FIELD_RATE_ID = 'rate_id';
28+
29+
/**
30+
* @var ResourceConnection
31+
*/
32+
private ResourceConnection $resourceConnection;
33+
34+
/**
35+
* @param ResourceConnection $resourceConnection
36+
*/
37+
public function __construct(ResourceConnection $resourceConnection)
38+
{
39+
$this->resourceConnection = $resourceConnection;
40+
}
41+
42+
/**
43+
* Remove shipping rates
44+
*
45+
* @param Rate[] $rates
46+
* @return void
47+
*/
48+
public function execute(array $rates): void
49+
{
50+
if (empty($rates)) {
51+
return;
52+
}
53+
$this->resourceConnection->getConnection()->delete(
54+
$this->resourceConnection->getTableName(self::TABLE),
55+
[
56+
self::FIELD_RATE_ID . ' IN (?)' => array_map(
57+
function ($rate) {
58+
return $rate->getId();
59+
},
60+
$rates
61+
)
62+
]
63+
);
64+
}
65+
}

0 commit comments

Comments
 (0)