Skip to content

Commit 8da351c

Browse files
ACPT-1552
Made new classes for caching region models and country models for AbstraceAddress
1 parent 643a0c7 commit 8da351c

File tree

3 files changed

+134
-16
lines changed

3 files changed

+134
-16
lines changed

app/code/Magento/Customer/Model/Address/AbstractAddress.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1212
use Magento\Customer\Api\Data\RegionInterface;
1313
use Magento\Customer\Api\Data\RegionInterfaceFactory;
14+
use Magento\Customer\Model\Address\AbstractAddress\CountryModelsCache;
15+
use Magento\Customer\Model\Address\AbstractAddress\RegionModelsCache;
1416
use Magento\Customer\Model\Data\Address as AddressData;
1517
use Magento\Framework\App\ObjectManager;
1618
use Magento\Framework\Model\AbstractExtensibleModel;
@@ -61,18 +63,28 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
6163
protected $_eventObject = 'customer_address';
6264

6365
/**
64-
* cache of Directory country models
66+
* Directory country models
6567
*
6668
* @var \Magento\Directory\Model\Country[]
69+
* @deprecated
70+
* @see $countryModelsCache
6771
*/
68-
protected $_countryModels = [];
72+
protected static $_countryModels = [];
73+
74+
/** @var CountryModelsCache */
75+
private readonly CountryModelsCache $countryModelsCache;
6976

7077
/**
71-
* cache of Directory region models
78+
* Directory region models
7279
*
7380
* @var \Magento\Directory\Model\Region[]
81+
* @deprecated
82+
* @see $regionModelsCache
7483
*/
75-
protected $_regionModels = [];
84+
protected static $_regionModels = [];
85+
86+
/** @var RegionModelsCache */
87+
private readonly RegionModelsCache $regionModelsCache;
7688

7789
/**
7890
* @var \Magento\Directory\Helper\Data
@@ -144,7 +156,9 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
144156
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
145157
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
146158
* @param array $data
147-
* @param CompositeValidator $compositeValidator
159+
* @param CompositeValidator|null $compositeValidator
160+
* @param CountryModelsCache|null $countryModelsCache
161+
* @param RegionModelsCache|null $regionModelsCache
148162
*
149163
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
150164
*/
@@ -165,7 +179,9 @@ public function __construct(
165179
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
166180
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
167181
array $data = [],
168-
CompositeValidator $compositeValidator = null
182+
?CompositeValidator $compositeValidator = null,
183+
?CountryModelsCache $countryModelsCache = null,
184+
?RegionModelsCache $regionModelsCache = null,
169185
) {
170186
$this->_directoryData = $directoryData;
171187
$data = $this->_implodeArrayField($data);
@@ -179,6 +195,10 @@ public function __construct(
179195
$this->dataObjectHelper = $dataObjectHelper;
180196
$this->compositeValidator = $compositeValidator ?: ObjectManager::getInstance()
181197
->get(CompositeValidator::class);
198+
$this->countryModelsCache = $countryModelsCache ?: ObjectManager::getInstance()
199+
->get(CountryModelsCache::class);
200+
$this->regionModelsCache = $regionModelsCache ?: ObjectManager::getInstance()
201+
->get(RegionModelsCache::class);
182202
parent::__construct(
183203
$context,
184204
$registry,
@@ -502,12 +522,12 @@ public function getCountry()
502522
*/
503523
public function getCountryModel()
504524
{
505-
if (!isset($this->_countryModels[$this->getCountryId()])) {
525+
if ($country = $this->countryModelsCache->get($this->getCountryId())) {
506526
$country = $this->_createCountryInstance();
507527
$country->load($this->getCountryId());
508-
$this->_countryModels[$this->getCountryId()] = $country;
528+
$this->countryModelsCache->add($this->getCountryId(), $country);
509529
}
510-
return $this->_countryModels[$this->getCountryId()];
530+
return $country;
511531
}
512532

513533
/**
@@ -521,14 +541,12 @@ public function getRegionModel($regionId = null)
521541
if ($regionId === null) {
522542
$regionId = $this->getRegionId();
523543
}
524-
525-
if (!isset($this->_regionModels[$regionId])) {
544+
if ($region = $this->regionModelsCache->get($regionId)) {
526545
$region = $this->_createRegionInstance();
527546
$region->load($regionId);
528-
$this->_regionModels[$regionId] = $region;
547+
$this->regionModelsCache->add($regionId, $region);
529548
}
530-
531-
return $this->_regionModels[$regionId];
549+
return $region;
532550
}
533551

534552
/**
@@ -746,7 +764,7 @@ private function processCustomAttribute(array $attribute): array
746764
*/
747765
public function _resetState(): void
748766
{
749-
$this->_countryModels = [];
750-
$this->_regionModels = [];
767+
self::$_countryModels = [];
768+
self::$_regionModels = [];
751769
}
752770
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Model\Address\AbstractAddress;
8+
9+
use Magento\Directory\Model\Country;
10+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
11+
12+
/**
13+
* Cache of Country Models
14+
*/
15+
class CountryModelsCache implements ResetAfterRequestInterface
16+
{
17+
/** @var array */
18+
private array $countryModels = [];
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function _resetState(): void
24+
{
25+
$this->countryModels = [];
26+
}
27+
28+
/**
29+
* Adds model to cache using key
30+
*
31+
* @param string $key
32+
* @param Country $value
33+
* @return void
34+
*/
35+
public function add(string $key, Country $model) : void
36+
{
37+
$this->countryModels[$key] = $model;
38+
}
39+
40+
/**
41+
* Gets model from cache using key
42+
*
43+
* @param string $key
44+
* @return Country|null
45+
*/
46+
public function get(string $key) : ?Country
47+
{
48+
return $this->countryModels[$key] ?? null;
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Model\Address\AbstractAddress;
8+
9+
use Magento\Directory\Model\Region;
10+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
11+
12+
/**
13+
* Cache of Region Models
14+
*/
15+
class RegionModelsCache implements ResetAfterRequestInterface
16+
{
17+
/** @var array */
18+
private array $regionModels = [];
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function _resetState(): void
24+
{
25+
$this->regionModels = [];
26+
}
27+
28+
/**
29+
* Adds model to cache using key
30+
*
31+
* @param string $key
32+
* @param Region $value
33+
* @return void
34+
*/
35+
public function add(string $key, Region $model) : void
36+
{
37+
$this->regionModels[$key] = $model;
38+
}
39+
40+
/**
41+
* Gets model from cache using key
42+
*
43+
* @param string $key
44+
* @return Region|null
45+
*/
46+
public function get(string $key) : ?Region
47+
{
48+
return $this->regionModels[$key] ?? null;
49+
}
50+
}

0 commit comments

Comments
 (0)