1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Eav\Model\Entity;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Cache\StateInterface;
/**
* Class AttributeCache
*/
class AttributeCache
{
/** cache prefix */
const ATTRIBUTES_CACHE_PREFIX = 'ATTRIBUTE_INSTANCES_CACHE';
/**
* @var CacheInterface
*/
private $cache;
/**
* @var StateInterface
*/
private $state;
/**
* @var AbstractAttribute[][]
*/
private $attributeInstances;
/**
* @var bool
*/
private $isAttributeCacheEnabled;
/**
* @var array
*/
private $unsupportedTypes;
/**
* AttributeCache constructor.
* @param CacheInterface $cache
* @param StateInterface $state
* @param array $unsupportedTypes
*/
public function __construct(
CacheInterface $cache,
StateInterface $state,
$unsupportedTypes = []
) {
$this->cache = $cache;
$this->state = $state;
$this->unsupportedTypes = $unsupportedTypes;
}
/**
* @return bool
*/
private function isAttributeCacheEnabled()
{
if ($this->isAttributeCacheEnabled === null) {
$this->isAttributeCacheEnabled = $this->state->isEnabled(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER);
}
return $this->isAttributeCacheEnabled;
}
/**
* Return attributes from cache
*
* @param string $entityType
* @param string $suffix
* @return object[]
*/
public function getAttributes($entityType, $suffix = '')
{
if (in_array($entityType, $this->unsupportedTypes)) {
return false;
}
if (isset($this->attributeInstances[$entityType . $suffix])) {
return $this->attributeInstances[$entityType . $suffix];
}
if ($this->isAttributeCacheEnabled()) {
$cacheKey = self::ATTRIBUTES_CACHE_PREFIX . $entityType . $suffix;
$attributesData = $this->cache->load($cacheKey);
if ($attributesData) {
$attributes = unserialize($attributesData);
$this->attributeInstances[$entityType . $suffix] = $attributes;
return $attributes;
}
}
return false;
}
/**
* Save attributes to cache
*
* @param string $entityType
* @param object[] $attributes
* @param string $suffix
* @return bool
*/
public function saveAttributes($entityType, $attributes, $suffix = '')
{
if (in_array($entityType, $this->unsupportedTypes)) {
return true;
}
$this->attributeInstances[$entityType . $suffix] = $attributes;
if ($this->isAttributeCacheEnabled()) {
$cacheKey = self::ATTRIBUTES_CACHE_PREFIX . $entityType . $suffix;
$attributesData = serialize($attributes);
$this->cache->save(
$attributesData,
$cacheKey,
[
\Magento\Eav\Model\Cache\Type::CACHE_TAG,
\Magento\Eav\Model\Entity\Attribute::CACHE_TAG,
\Magento\Framework\App\Config\ScopePool::CACHE_TAG
]
);
}
return true;
}
/**
* Clear attributes cache
*
* @return bool
*/
public function clear()
{
unset($this->attributeInstances);
if ($this->isAttributeCacheEnabled()) {
$this->cache->clean(
[
\Magento\Eav\Model\Cache\Type::CACHE_TAG,
\Magento\Eav\Model\Entity\Attribute::CACHE_TAG,
]
);
}
return true;
}
}