Skip to content

Commit 6e0a786

Browse files
authored
Merge pull request #863 from magento-jackalopes/MAGETWO-64224-remove-usages-of-attributecache
[Jackalopes] Magetwo 64224 remove usages of attributecache
2 parents 131ada4 + 22500da commit 6e0a786

File tree

13 files changed

+1226
-82
lines changed

13 files changed

+1226
-82
lines changed

app/code/Magento/Customer/Model/Attribute.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class Attribute extends \Magento\Eav\Model\Attribute
4242
protected $indexerRegistry;
4343

4444
/**
45+
* @var \Magento\Customer\Model\Metadata\AttributeMetadataCache
46+
*/
47+
private $attributeMetadataCache;
48+
49+
/**
50+
* Constructor
51+
*
4552
* @param \Magento\Framework\Model\Context $context
4653
* @param \Magento\Framework\Registry $registry
4754
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
@@ -61,8 +68,8 @@ class Attribute extends \Magento\Eav\Model\Attribute
6168
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
6269
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
6370
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
64-
* @param array $data
65-
*
71+
* @param array|null $data
72+
* @param \Magento\Customer\Model\Metadata\AttributeMetadataCache|null $attributeMetadataCache
6673
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6774
*/
6875
public function __construct(
@@ -85,9 +92,12 @@ public function __construct(
8592
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
8693
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
8794
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
88-
array $data = []
95+
array $data = [],
96+
\Magento\Customer\Model\Metadata\AttributeMetadataCache $attributeMetadataCache = null
8997
) {
9098
$this->indexerRegistry = $indexerRegistry;
99+
$this->attributeMetadataCache = $attributeMetadataCache ?: \Magento\Framework\App\ObjectManager::getInstance()
100+
->get(\Magento\Customer\Model\Metadata\AttributeMetadataCache::class);
91101
parent::__construct(
92102
$context,
93103
$registry,
@@ -122,9 +132,7 @@ protected function _construct()
122132
}
123133

124134
/**
125-
* Processing object after save data
126-
*
127-
* @return $this
135+
* @inheritdoc
128136
*/
129137
public function afterSave()
130138
{
@@ -133,9 +141,19 @@ public function afterSave()
133141
} elseif (!$this->isObjectNew() && $this->dataHasChangedFor(EavAttributeInterface::IS_USED_IN_GRID)) {
134142
$this->_getResource()->addCommitCallback([$this, 'invalidate']);
135143
}
144+
$this->attributeMetadataCache->clean();
136145
return parent::afterSave();
137146
}
138147

148+
/**
149+
* @inheritdoc
150+
*/
151+
public function afterDelete()
152+
{
153+
$this->attributeMetadataCache->clean();
154+
return parent::afterDelete();
155+
}
156+
139157
/**
140158
* Init indexing process after customer delete
141159
*

app/code/Magento/Customer/Model/Metadata/AddressCachedMetadata.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Customer\Model\Metadata;
87

98
use Magento\Customer\Api\AddressMetadataInterface;
@@ -19,12 +18,15 @@ class AddressCachedMetadata extends CachedMetadata implements AddressMetadataInt
1918
protected $entityType = 'customer_address';
2019

2120
/**
22-
* Initialize dependencies.
21+
* Constructor
2322
*
2423
* @param AddressMetadata $metadata
24+
* @param AttributeMetadataCache|null $attributeMetadataCache
2525
*/
26-
public function __construct(AddressMetadata $metadata)
27-
{
28-
$this->metadata = $metadata;
26+
public function __construct(
27+
AddressMetadata $metadata,
28+
AttributeMetadataCache $attributeMetadataCache = null
29+
) {
30+
parent::__construct($metadata, $attributeMetadataCache);
2931
}
3032
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Model\Metadata;
7+
8+
use Magento\Framework\App\CacheInterface;
9+
use Magento\Framework\App\Cache\StateInterface;
10+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
11+
use Magento\Framework\Serialize\SerializerInterface;
12+
use Magento\Eav\Model\Cache\Type;
13+
use Magento\Eav\Model\Entity\Attribute;
14+
use Magento\Config\App\Config\Type\System;
15+
16+
/**
17+
* Cache for attribute metadata
18+
*/
19+
class AttributeMetadataCache
20+
{
21+
/**
22+
* Cache prefix
23+
*/
24+
const ATTRIBUTE_METADATA_CACHE_PREFIX = 'ATTRIBUTE_METADATA_INSTANCES_CACHE';
25+
26+
/**
27+
* @var CacheInterface
28+
*/
29+
private $cache;
30+
31+
/**
32+
* @var StateInterface
33+
*/
34+
private $state;
35+
36+
/**
37+
* @var AttributeMetadataInterface[]
38+
*/
39+
private $attributes;
40+
41+
/**
42+
* @var bool
43+
*/
44+
private $isAttributeCacheEnabled;
45+
46+
/**
47+
* @var AttributeMetadataHydrator
48+
*/
49+
private $attributeMetadataHydrator;
50+
51+
/**
52+
* @var SerializerInterface
53+
*/
54+
private $serializer;
55+
56+
/**
57+
* Constructor
58+
*
59+
* @param CacheInterface $cache
60+
* @param StateInterface $state
61+
* @param SerializerInterface $serializer
62+
* @param AttributeMetadataHydrator $attributeMetadataHydrator
63+
*/
64+
public function __construct(
65+
CacheInterface $cache,
66+
StateInterface $state,
67+
SerializerInterface $serializer,
68+
AttributeMetadataHydrator $attributeMetadataHydrator
69+
) {
70+
$this->cache = $cache;
71+
$this->state = $state;
72+
$this->serializer = $serializer;
73+
$this->attributeMetadataHydrator = $attributeMetadataHydrator;
74+
}
75+
76+
/**
77+
* Load attributes metadata from cache
78+
*
79+
* @param string $entityType
80+
* @param string $suffix
81+
* @return AttributeMetadataInterface[]|bool
82+
*/
83+
public function load($entityType, $suffix = '')
84+
{
85+
if (isset($this->attributes[$entityType . $suffix])) {
86+
return $this->attributes[$entityType . $suffix];
87+
}
88+
if ($this->isEnabled()) {
89+
$cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix;
90+
$serializedData = $this->cache->load($cacheKey);
91+
if ($serializedData) {
92+
$attributesData = $this->serializer->unserialize($serializedData);
93+
$attributes = [];
94+
foreach ($attributesData as $key => $attributeData) {
95+
$attributes[$key] = $this->attributeMetadataHydrator->hydrate($attributeData);
96+
}
97+
$this->attributes[$entityType . $suffix] = $attributes;
98+
return $attributes;
99+
}
100+
}
101+
return false;
102+
}
103+
104+
/**
105+
* Save attributes metadata to cache
106+
*
107+
* @param string $entityType
108+
* @param AttributeMetadataInterface[] $attributes
109+
* @param string $suffix
110+
* @return void
111+
*/
112+
public function save($entityType, array $attributes, $suffix = '')
113+
{
114+
$this->attributes[$entityType . $suffix] = $attributes;
115+
if ($this->isEnabled()) {
116+
$cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix;
117+
$attributesData = [];
118+
foreach ($attributes as $key => $attribute) {
119+
$attributesData[$key] = $this->attributeMetadataHydrator->extract($attribute);
120+
}
121+
$serializedData = $this->serializer->serialize($attributesData);
122+
$this->cache->save(
123+
$serializedData,
124+
$cacheKey,
125+
[
126+
Type::CACHE_TAG,
127+
Attribute::CACHE_TAG,
128+
System::CACHE_TAG
129+
]
130+
);
131+
}
132+
}
133+
134+
/**
135+
* Clean attributes metadata cache
136+
*
137+
* @return void
138+
*/
139+
public function clean()
140+
{
141+
unset($this->attributes);
142+
if ($this->isEnabled()) {
143+
$this->cache->clean(
144+
[
145+
Type::CACHE_TAG,
146+
Attribute::CACHE_TAG,
147+
]
148+
);
149+
}
150+
}
151+
152+
/**
153+
* Check if cache enabled
154+
*
155+
* @return bool
156+
*/
157+
private function isEnabled()
158+
{
159+
if (null === $this->isAttributeCacheEnabled) {
160+
$this->isAttributeCacheEnabled = $this->state->isEnabled(Type::TYPE_IDENTIFIER);
161+
}
162+
return $this->isAttributeCacheEnabled;
163+
}
164+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Model\Metadata;
7+
8+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
9+
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
10+
use Magento\Customer\Api\Data\OptionInterface;
11+
use Magento\Customer\Api\Data\OptionInterfaceFactory;
12+
use Magento\Customer\Api\Data\ValidationRuleInterface;
13+
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
14+
use Magento\Framework\Reflection\DataObjectProcessor;
15+
16+
/**
17+
* Hydrator for AttributeMetadataInterface
18+
*/
19+
class AttributeMetadataHydrator
20+
{
21+
/**
22+
* @var AttributeMetadataInterfaceFactory
23+
*/
24+
private $attributeMetadataFactory;
25+
26+
/**
27+
* @var OptionInterfaceFactory
28+
*/
29+
private $optionFactory;
30+
31+
/**
32+
* @var ValidationRuleInterfaceFactory
33+
*/
34+
private $validationRuleFactory;
35+
36+
/**
37+
* @var DataObjectProcessor
38+
*/
39+
private $dataObjectProcessor;
40+
41+
/**
42+
* Constructor
43+
*
44+
* @param AttributeMetadataInterfaceFactory $attributeMetadataFactory
45+
* @param OptionInterfaceFactory $optionFactory
46+
* @param ValidationRuleInterfaceFactory $validationRuleFactory
47+
* @param DataObjectProcessor $dataObjectProcessor
48+
*/
49+
public function __construct(
50+
AttributeMetadataInterfaceFactory $attributeMetadataFactory,
51+
OptionInterfaceFactory $optionFactory,
52+
ValidationRuleInterfaceFactory $validationRuleFactory,
53+
DataObjectProcessor $dataObjectProcessor
54+
) {
55+
$this->attributeMetadataFactory = $attributeMetadataFactory;
56+
$this->optionFactory = $optionFactory;
57+
$this->validationRuleFactory = $validationRuleFactory;
58+
$this->dataObjectProcessor = $dataObjectProcessor;
59+
}
60+
61+
/**
62+
* Convert array to AttributeMetadataInterface
63+
*
64+
* @param array $data
65+
* @return AttributeMetadataInterface
66+
*/
67+
public function hydrate(array $data)
68+
{
69+
if (isset($data[AttributeMetadataInterface::OPTIONS])) {
70+
$data[AttributeMetadataInterface::OPTIONS] = $this->createOptions(
71+
$data[AttributeMetadataInterface::OPTIONS]
72+
);
73+
}
74+
if (isset($data[AttributeMetadataInterface::VALIDATION_RULES])) {
75+
$data[AttributeMetadataInterface::VALIDATION_RULES] = $this->createValidationRules(
76+
$data[AttributeMetadataInterface::VALIDATION_RULES]
77+
);
78+
}
79+
return $this->attributeMetadataFactory->create(['data' => $data]);
80+
}
81+
82+
/**
83+
* Populate options with data
84+
*
85+
* @param array $data
86+
* @return OptionInterface[]
87+
*/
88+
private function createOptions(array $data)
89+
{
90+
foreach ($data as $key => $optionData) {
91+
if (isset($optionData[OptionInterface::OPTIONS])) {
92+
$optionData[OptionInterface::OPTIONS] = $this->createOptions($optionData[OptionInterface::OPTIONS]);
93+
}
94+
$data[$key] = $this->optionFactory->create(['data' => $optionData]);
95+
}
96+
return $data;
97+
}
98+
99+
/**
100+
* Populate validation rules with data
101+
*
102+
* @param array $data
103+
* @return ValidationRuleInterface[]
104+
*/
105+
private function createValidationRules(array $data)
106+
{
107+
foreach ($data as $key => $validationRuleData) {
108+
$data[$key] = $this->validationRuleFactory->create(['data' => $validationRuleData]);
109+
}
110+
return $data;
111+
}
112+
113+
/**
114+
* Convert AttributeMetadataInterface to array
115+
*
116+
* @param AttributeMetadataInterface $attributeMetadata
117+
* @return array
118+
*/
119+
public function extract($attributeMetadata)
120+
{
121+
return $this->dataObjectProcessor->buildOutputDataArray(
122+
$attributeMetadata,
123+
AttributeMetadataInterface::class
124+
);
125+
}
126+
}

0 commit comments

Comments
 (0)