AttributeMetadataCache.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Metadata;
  7. use Magento\Config\App\Config\Type\System;
  8. use Magento\Customer\Api\Data\AttributeMetadataInterface;
  9. use Magento\Eav\Model\Cache\Type;
  10. use Magento\Eav\Model\Entity\Attribute;
  11. use Magento\Framework\App\Cache\StateInterface;
  12. use Magento\Framework\App\CacheInterface;
  13. use Magento\Framework\Serialize\SerializerInterface;
  14. use Magento\Store\Model\StoreManagerInterface;
  15. /**
  16. * Cache for attribute metadata
  17. */
  18. class AttributeMetadataCache
  19. {
  20. /**
  21. * Cache prefix
  22. */
  23. const ATTRIBUTE_METADATA_CACHE_PREFIX = 'ATTRIBUTE_METADATA_INSTANCES_CACHE';
  24. /**
  25. * @var CacheInterface
  26. */
  27. private $cache;
  28. /**
  29. * @var StateInterface
  30. */
  31. private $state;
  32. /**
  33. * @var AttributeMetadataInterface[]
  34. */
  35. private $attributes;
  36. /**
  37. * @var bool
  38. */
  39. private $isAttributeCacheEnabled;
  40. /**
  41. * @var AttributeMetadataHydrator
  42. */
  43. private $attributeMetadataHydrator;
  44. /**
  45. * @var SerializerInterface
  46. */
  47. private $serializer;
  48. /**
  49. * @var StoreManagerInterface
  50. */
  51. private $storeManager;
  52. /**
  53. * Constructor
  54. *
  55. * @param CacheInterface $cache
  56. * @param StateInterface $state
  57. * @param SerializerInterface $serializer
  58. * @param AttributeMetadataHydrator $attributeMetadataHydrator
  59. * @param StoreManagerInterface $storeManager
  60. */
  61. public function __construct(
  62. CacheInterface $cache,
  63. StateInterface $state,
  64. SerializerInterface $serializer,
  65. AttributeMetadataHydrator $attributeMetadataHydrator,
  66. StoreManagerInterface $storeManager = null
  67. ) {
  68. $this->cache = $cache;
  69. $this->state = $state;
  70. $this->serializer = $serializer;
  71. $this->attributeMetadataHydrator = $attributeMetadataHydrator;
  72. $this->storeManager = $storeManager ?: \Magento\Framework\App\ObjectManager::getInstance()
  73. ->get(StoreManagerInterface::class);
  74. }
  75. /**
  76. * Load attributes metadata from cache
  77. *
  78. * @param string $entityType
  79. * @param string $suffix
  80. * @return AttributeMetadataInterface[]|bool
  81. */
  82. public function load($entityType, $suffix = '')
  83. {
  84. $storeId = $this->storeManager->getStore()->getId();
  85. if (isset($this->attributes[$entityType . $suffix . $storeId])) {
  86. return $this->attributes[$entityType . $suffix . $storeId];
  87. }
  88. if ($this->isEnabled()) {
  89. $cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix . $storeId;
  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 . $storeId] = $attributes;
  98. return $attributes;
  99. }
  100. }
  101. return false;
  102. }
  103. /**
  104. * Save attributes metadata to cache
  105. *
  106. * @param string $entityType
  107. * @param AttributeMetadataInterface[] $attributes
  108. * @param string $suffix
  109. * @return void
  110. */
  111. public function save($entityType, array $attributes, $suffix = '')
  112. {
  113. $storeId = $this->storeManager->getStore()->getId();
  114. $this->attributes[$entityType . $suffix . $storeId] = $attributes;
  115. if ($this->isEnabled()) {
  116. $cacheKey = self::ATTRIBUTE_METADATA_CACHE_PREFIX . $entityType . $suffix . $storeId;
  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. * Clean attributes metadata cache
  135. *
  136. * @return void
  137. */
  138. public function clean()
  139. {
  140. unset($this->attributes);
  141. if ($this->isEnabled()) {
  142. $this->cache->clean(
  143. [
  144. Type::CACHE_TAG,
  145. Attribute::CACHE_TAG,
  146. ]
  147. );
  148. }
  149. }
  150. /**
  151. * Check if cache enabled
  152. *
  153. * @return bool
  154. */
  155. private function isEnabled()
  156. {
  157. if (null === $this->isAttributeCacheEnabled) {
  158. $this->isAttributeCacheEnabled = $this->state->isEnabled(Type::TYPE_IDENTIFIER);
  159. }
  160. return $this->isAttributeCacheEnabled;
  161. }
  162. }