Skip to content

Commit e51403a

Browse files
author
Igor Melnikov
committed
MAGETWO-64224: Remove usages of AttributeCache from customer module
- replacing AttributeCache with AttributeMetadataCache
1 parent f3aae83 commit e51403a

File tree

6 files changed

+639
-36
lines changed

6 files changed

+639
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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\Customer\Api\Data\AttributeMetadataInterfaceFactory;
12+
use Magento\Customer\Api\Data\OptionInterface;
13+
use Magento\Customer\Api\Data\OptionInterfaceFactory;
14+
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
15+
use Magento\Framework\Serialize\SerializerInterface;
16+
use Magento\Eav\Model\Cache\Type;
17+
use Magento\Eav\Model\Entity\Attribute;
18+
use Magento\Config\App\Config\Type\System;
19+
20+
/**
21+
* Cache for attribute metadata
22+
*/
23+
class AttributeMetadataCache
24+
{
25+
/**
26+
* Cache prefix
27+
*/
28+
const ATTRIBUTE_METADATA_CACHE_PREFIX = 'ATTRIBUTE_METADATA_INSTANCES_CACHE';
29+
30+
/**
31+
* @var CacheInterface
32+
*/
33+
private $cache;
34+
35+
/**
36+
* @var StateInterface
37+
*/
38+
private $state;
39+
40+
/**
41+
* @var AttributeMetadataInterface[]
42+
*/
43+
private $attributes;
44+
45+
/**
46+
* @var bool
47+
*/
48+
private $isAttributeCacheEnabled;
49+
50+
/**
51+
* @var AttributeMetadataInterfaceFactory
52+
*/
53+
private $attributeMetadataFactory;
54+
55+
/**
56+
* @var OptionInterfaceFactory
57+
*/
58+
private $optionFactory;
59+
60+
/**
61+
* @var ValidationRuleInterfaceFactory
62+
*/
63+
private $validationRuleFactory;
64+
65+
/**
66+
* @var SerializerInterface
67+
*/
68+
private $serializer;
69+
70+
/**
71+
* Constructor
72+
*
73+
* @param CacheInterface $cache
74+
* @param StateInterface $state
75+
* @param AttributeMetadataInterfaceFactory $attributeMetadataFactory
76+
* @param OptionInterfaceFactory $optionFactory
77+
* @param ValidationRuleInterfaceFactory $validationRuleFactory
78+
* @param SerializerInterface $serializer
79+
*/
80+
public function __construct(
81+
CacheInterface $cache,
82+
StateInterface $state,
83+
AttributeMetadataInterfaceFactory $attributeMetadataFactory,
84+
OptionInterfaceFactory $optionFactory,
85+
ValidationRuleInterfaceFactory $validationRuleFactory,
86+
SerializerInterface $serializer
87+
) {
88+
$this->cache = $cache;
89+
$this->state = $state;
90+
$this->attributeMetadataFactory = $attributeMetadataFactory;
91+
$this->optionFactory = $optionFactory;
92+
$this->validationRuleFactory = $validationRuleFactory;
93+
$this->serializer = $serializer;
94+
}
95+
96+
/**
97+
* Load attributes metadata from cache
98+
*
99+
* @param string $entityType
100+
* @param string $suffix
101+
* @return AttributeMetadataInterface[]|bool
102+
*/
103+
public function load($entityType, $suffix = '')
104+
{
105+
if (isset($this->attributes[$entityType . $suffix])) {
106+
return $this->attributes[$entityType . $suffix];
107+
}
108+
if ($this->isEnabled()) {
109+
$cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix;
110+
$serializedData = $this->cache->load($cacheKey);
111+
if ($serializedData) {
112+
$attributesData = $this->serializer->unserialize($serializedData);
113+
$attributes = [];
114+
foreach ($attributesData as $key => $attributeData) {
115+
$attributes[$key] = $this->createMetadataAttribute($attributeData);
116+
}
117+
$this->attributes[$entityType . $suffix] = $attributes;
118+
return $attributes;
119+
}
120+
}
121+
return false;
122+
}
123+
124+
/**
125+
* Save attributes metadata to cache
126+
*
127+
* @param string $entityType
128+
* @param AttributeMetadataInterface[] $attributes
129+
* @param string $suffix
130+
* @return void
131+
*/
132+
public function save($entityType, $attributes, $suffix = '')
133+
{
134+
$this->attributes[$entityType . $suffix] = $attributes;
135+
if ($this->isEnabled()) {
136+
$cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix;
137+
$attributesData = [];
138+
foreach ($attributes as $key => $attribute) {
139+
$attributesData[$key] = $attribute->__toArray();
140+
}
141+
$serializedData = $this->serializer->serialize($attributesData);
142+
$this->cache->save(
143+
$serializedData,
144+
$cacheKey,
145+
[
146+
Type::CACHE_TAG,
147+
Attribute::CACHE_TAG,
148+
System::CACHE_TAG
149+
]
150+
);
151+
}
152+
}
153+
154+
/**
155+
* Clean attributes metadata cache
156+
*
157+
* @return void
158+
*/
159+
public function clean()
160+
{
161+
unset($this->attributes);
162+
if ($this->isEnabled()) {
163+
$this->cache->clean(
164+
[
165+
Type::CACHE_TAG,
166+
Attribute::CACHE_TAG,
167+
]
168+
);
169+
}
170+
}
171+
172+
/**
173+
* Check if cache enabled
174+
*
175+
* @return bool
176+
*/
177+
private function isEnabled()
178+
{
179+
if (null === $this->isAttributeCacheEnabled) {
180+
$this->isAttributeCacheEnabled = $this->state->isEnabled(Type::TYPE_IDENTIFIER);
181+
}
182+
return $this->isAttributeCacheEnabled;
183+
}
184+
185+
/**
186+
* Create and populate with data AttributeMetadataInterface
187+
*
188+
* @param array $data
189+
* @return AttributeMetadataInterface
190+
*/
191+
private function createMetadataAttribute($data)
192+
{
193+
if (isset($data[AttributeMetadataInterface::OPTIONS])) {
194+
$data[AttributeMetadataInterface::OPTIONS] = $this->createOptions(
195+
$data[AttributeMetadataInterface::OPTIONS]
196+
);
197+
}
198+
if (isset($data[AttributeMetadataInterface::VALIDATION_RULES])) {
199+
$validationRules = [];
200+
foreach ($data[AttributeMetadataInterface::VALIDATION_RULES] as $validationRuleData) {
201+
$validationRules[] = $this->validationRuleFactory->create(['data' => $validationRuleData]);
202+
}
203+
$data[AttributeMetadataInterface::VALIDATION_RULES] = $validationRules;
204+
}
205+
return $this->attributeMetadataFactory->create(['data' => $data]);
206+
}
207+
208+
/**
209+
* Create and populate with data OptionInterface
210+
*
211+
* @param array $data
212+
* @return OptionInterface[]
213+
*/
214+
private function createOptions($data)
215+
{
216+
foreach ($data as $key => $optionData) {
217+
if (isset($optionData[OptionInterface::OPTIONS])) {
218+
$optionData[OptionInterface::OPTIONS] = $this->createOptions($optionData[OptionInterface::OPTIONS]);
219+
}
220+
$data[$key] = $this->optionFactory->create(['data' => $optionData]);
221+
}
222+
return $data;
223+
}
224+
}

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

+22-32
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
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\MetadataInterface;
10-
use Magento\Eav\Model\Entity\AttributeCache;
119
use Magento\Framework\App\ObjectManager;
1210

1311
/**
@@ -23,36 +21,41 @@ class CachedMetadata implements MetadataInterface
2321
protected $entityType = 'none';
2422

2523
/**
26-
* @var AttributeCache
24+
* @var AttributeMetadataCache
2725
*/
28-
private $cache;
26+
private $attributeMetadataCache;
2927

3028
/**
3129
* @var MetadataInterface
3230
*/
3331
protected $metadata;
3432

3533
/**
36-
* Initialize dependencies.
34+
* Constructor
3735
*
3836
* @param MetadataInterface $metadata
37+
* @param AttributeMetadataCache|null $attributeMetadataCache
3938
*/
40-
public function __construct(MetadataInterface $metadata)
41-
{
39+
public function __construct(
40+
MetadataInterface $metadata,
41+
AttributeMetadataCache $attributeMetadataCache = null
42+
) {
4243
$this->metadata = $metadata;
44+
$this->attributeMetadataCache = $attributeMetadataCache ?: ObjectManager::getInstance()
45+
->get(AttributeMetadataCache::class);
4346
}
4447

4548
/**
4649
* {@inheritdoc}
4750
*/
4851
public function getAttributes($formCode)
4952
{
50-
$attributes = $this->getCache()->getAttributes($this->entityType, $formCode);
53+
$attributes = $this->attributeMetadataCache->load($this->entityType, $formCode);
5154
if ($attributes !== false) {
5255
return $attributes;
5356
}
5457
$attributes = $this->metadata->getAttributes($formCode);
55-
$this->getCache()->saveAttributes($this->entityType, $attributes, $formCode);
58+
$this->attributeMetadataCache->save($this->entityType, $attributes, $formCode);
5659
return $attributes;
5760
}
5861

@@ -61,26 +64,26 @@ public function getAttributes($formCode)
6164
*/
6265
public function getAttributeMetadata($attributeCode)
6366
{
64-
$metadata = $this->getCache()->getAttributes($this->entityType, $attributeCode);
65-
if ($metadata) {
66-
return $metadata;
67+
$attributesMetadata = $this->attributeMetadataCache->load($this->entityType, $attributeCode);
68+
if (false !== $attributesMetadata) {
69+
return array_shift($attributesMetadata);
6770
}
68-
$metadata = $this->metadata->getAttributeMetadata($attributeCode);
69-
$this->getCache()->saveAttributes($this->entityType, $metadata, $attributeCode);
70-
return $metadata;
71+
$attributeMetadata = $this->metadata->getAttributeMetadata($attributeCode);
72+
$this->attributeMetadataCache->save($this->entityType, [$attributeMetadata], $attributeCode);
73+
return $attributeMetadata;
7174
}
7275

7376
/**
7477
* {@inheritdoc}
7578
*/
7679
public function getAllAttributesMetadata()
7780
{
78-
$attributes = $this->getCache()->getAttributes($this->entityType, 'all');
81+
$attributes = $this->attributeMetadataCache->load($this->entityType, 'all');
7982
if ($attributes !== false) {
8083
return $attributes;
8184
}
8285
$attributes = $this->metadata->getAllAttributesMetadata();
83-
$this->getCache()->saveAttributes($this->entityType, $attributes, 'all');
86+
$this->attributeMetadataCache->save($this->entityType, $attributes, 'all');
8487
return $attributes;
8588
}
8689

@@ -89,25 +92,12 @@ public function getAllAttributesMetadata()
8992
*/
9093
public function getCustomAttributesMetadata($dataObjectClassName = null)
9194
{
92-
$attributes = $this->getCache()->getAttributes($this->entityType, 'custom');
95+
$attributes = $this->attributeMetadataCache->load($this->entityType, 'custom');
9396
if ($attributes !== false) {
9497
return $attributes;
9598
}
9699
$attributes = $this->metadata->getCustomAttributesMetadata();
97-
$this->getCache()->saveAttributes($this->entityType, $attributes, 'custom');
100+
$this->attributeMetadataCache->save($this->entityType, $attributes, 'custom');
98101
return $attributes;
99102
}
100-
101-
/**
102-
* @return AttributeCache
103-
* @deprecated
104-
*/
105-
private function getCache()
106-
{
107-
if (!$this->cache) {
108-
$this->cache = ObjectManager::getInstance()->get(AttributeCache::class);
109-
}
110-
111-
return $this->cache;
112-
}
113103
}

0 commit comments

Comments
 (0)