Skip to content

Commit 4fce219

Browse files
committed
MC-16108: EAV attribute is not cached
- Add soft dependency of GiftMessage to EAV module;
1 parent 1ccc58f commit 4fce219

File tree

3 files changed

+82
-44
lines changed

3 files changed

+82
-44
lines changed

app/code/Magento/Catalog/Model/Config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Framework\Serialize\SerializerInterface;
1010

1111
/**
12+
* Catalog config model.
13+
*
1214
* @SuppressWarnings(PHPMD.LongVariable)
1315
* @SuppressWarnings(PHPMD.TooManyFields)
1416
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)

app/code/Magento/Eav/Model/Config.php

Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
99
use Magento\Eav\Model\Entity\Type;
1010
use Magento\Eav\Model\ResourceModel\Attribute\DefaultEntityAttributes\ProviderInterface;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
1112
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\Exception\LocalizedException;
1314
use Magento\Framework\Model\AbstractModel;
1415
use Magento\Framework\Serialize\SerializerInterface;
15-
use Magento\Framework\App\Config\ScopeConfigInterface;
1616

1717
/**
18+
* EAV config model.
19+
*
1820
* @api
21+
* @SuppressWarnings(PHPMD.TooManyFields)
1922
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2023
* @since 100.0.2
2124
*/
@@ -552,50 +555,9 @@ public function getAttribute($entityType, $code)
552555
}
553556

554557
if ($this->scopeConfig->getValue(self::XML_PATH_CACHE_USER_DEFINED_ATTRIBUTES)) {
555-
$cacheKey = self::ATTRIBUTES_CACHE_ID . '-attribute-' . $entityTypeCode . '-' . $code;
556-
$attributeData = $this->isCacheEnabled() && ($attribute = $this->_cache->load($cacheKey))
557-
? $this->serializer->unserialize($attribute)
558-
: null;
559-
if ($attributeData) {
560-
if (isset($attributeData['attribute_id'])) {
561-
$attribute = $this->_createAttribute($entityType, $attributeData);
562-
} else {
563-
$entityType = $this->getEntityType($entityType);
564-
$attribute = $this->createAttribute($entityType->getAttributeModel());
565-
$attribute->setAttributeCode($code);
566-
$attribute = $this->setAttributeData($attribute, $entityType);
567-
}
568-
} else {
569-
$attribute = $this->createAttributeByAttributeCode($entityType, $code);
570-
$this->_addAttributeReference(
571-
$attribute->getAttributeId(),
572-
$attribute->getAttributeCode(),
573-
$entityTypeCode
574-
);
575-
$this->saveAttribute($attribute, $entityTypeCode, $attribute->getAttributeCode());
576-
if ($this->isCacheEnabled()) {
577-
$this->_cache->save(
578-
$this->serializer->serialize($attribute->getData()),
579-
$cacheKey,
580-
[
581-
\Magento\Eav\Model\Cache\Type::CACHE_TAG,
582-
\Magento\Eav\Model\Entity\Attribute::CACHE_TAG
583-
]
584-
);
585-
}
586-
}
558+
$attribute = $this->cacheUserDefinedAttribute($entityType, $entityTypeCode, $code);
587559
} else {
588-
$attributes = $this->loadAttributes($entityTypeCode);
589-
$attribute = $attributes[$code] ?? null;
590-
if (!$attribute) {
591-
$attribute = $this->createAttributeByAttributeCode($entityType, $code);
592-
$this->_addAttributeReference(
593-
$attribute->getAttributeId(),
594-
$attribute->getAttributeCode(),
595-
$entityTypeCode
596-
);
597-
$this->saveAttribute($attribute, $entityTypeCode, $attribute->getAttributeCode());
598-
}
560+
$attribute = $this->initUserDefinedAttribute($entityType, $entityTypeCode, $code);
599561
}
600562

601563
\Magento\Framework\Profiler::stop('EAV: ' . __METHOD__);
@@ -668,6 +630,79 @@ private function initSystemAttributes($entityType, $systemAttributes)
668630
return $this;
669631
}
670632

633+
/**
634+
* Initialize user defined attribute from cache or cache it.
635+
*
636+
* @param string $entityType
637+
* @param mixed $entityTypeCode
638+
* @param string $code
639+
* @return AbstractAttribute
640+
* @throws LocalizedException
641+
*/
642+
private function cacheUserDefinedAttribute($entityType, $entityTypeCode, $code): AbstractAttribute
643+
{
644+
$cacheKey = self::ATTRIBUTES_CACHE_ID . '-attribute-' . $entityTypeCode . '-' . $code;
645+
$attributeData = $this->isCacheEnabled() && ($attribute = $this->_cache->load($cacheKey))
646+
? $this->serializer->unserialize($attribute)
647+
: null;
648+
if ($attributeData) {
649+
if (isset($attributeData['attribute_id'])) {
650+
$attribute = $this->_createAttribute($entityType, $attributeData);
651+
} else {
652+
$entityType = $this->getEntityType($entityType);
653+
$attribute = $this->createAttribute($entityType->getAttributeModel());
654+
$attribute->setAttributeCode($code);
655+
$attribute = $this->setAttributeData($attribute, $entityType);
656+
}
657+
} else {
658+
$attribute = $this->createAttributeByAttributeCode($entityType, $code);
659+
$this->_addAttributeReference(
660+
$attribute->getAttributeId(),
661+
$attribute->getAttributeCode(),
662+
$entityTypeCode
663+
);
664+
$this->saveAttribute($attribute, $entityTypeCode, $attribute->getAttributeCode());
665+
if ($this->isCacheEnabled()) {
666+
$this->_cache->save(
667+
$this->serializer->serialize($attribute->getData()),
668+
$cacheKey,
669+
[
670+
\Magento\Eav\Model\Cache\Type::CACHE_TAG,
671+
\Magento\Eav\Model\Entity\Attribute::CACHE_TAG
672+
]
673+
);
674+
}
675+
}
676+
677+
return $attribute;
678+
}
679+
680+
/**
681+
* Initialize user defined attribute and save it to memory cache.
682+
*
683+
* @param mixed $entityType
684+
* @param string $entityTypeCode
685+
* @param string $code
686+
* @return AbstractAttribute|null
687+
* @throws LocalizedException
688+
*/
689+
private function initUserDefinedAttribute($entityType, $entityTypeCode, $code): ?AbstractAttribute
690+
{
691+
$attributes = $this->loadAttributes($entityTypeCode);
692+
$attribute = $attributes[$code] ?? null;
693+
if (!$attribute) {
694+
$attribute = $this->createAttributeByAttributeCode($entityType, $code);
695+
$this->_addAttributeReference(
696+
$attribute->getAttributeId(),
697+
$attribute->getAttributeCode(),
698+
$entityTypeCode
699+
);
700+
$this->saveAttribute($attribute, $entityTypeCode, $attribute->getAttributeCode());
701+
}
702+
703+
return $attribute;
704+
}
705+
671706
/**
672707
* Create attribute
673708
*

app/code/Magento/GiftMessage/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"magento/module-ui": "*"
1818
},
1919
"suggest": {
20+
"magento/module-eav": "*",
2021
"magento/module-multishipping": "*"
2122
},
2223
"type": "magento2-module",

0 commit comments

Comments
 (0)