Skip to content

Commit

Permalink
Added EAV Config Cache (#2993)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhiendl authored Feb 17, 2023
1 parent 0010a07 commit fc97382
Show file tree
Hide file tree
Showing 31 changed files with 607 additions and 668 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,15 @@
"contributions": [
"code"
]
},
{
"login": "davidhiendl",
"name": "David Hiendl",
"avatar_url": "https://avatars.githubusercontent.com/u/11006964?v=4",
"profile": "https://github.com/davidhiendl",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align="center">
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
<a href="#contributors-"><img src="https://img.shields.io/badge/all_contributors-151-orange.svg" alt="All Contributors"></a>
<a href="#contributors-"><img src="https://img.shields.io/badge/all_contributors-152-orange.svg" alt="All Contributors"></a>
<!-- ALL-CONTRIBUTORS-BADGE:END -->
<a href="https://packagist.org/packages/openmage/magento-lts"><img src="https://poser.pugx.org/openmage/magento-lts/d/total.svg" alt="Total Downloads"></a>
<a href="https://packagist.org/packages/openmage/magento-lts"><img src="https://poser.pugx.org/openmage/magento-lts/license.svg" alt="License"></a>
Expand Down Expand Up @@ -207,6 +207,7 @@ Do not use 20.x.x if you need IE support.
- removed support for `global/sales/old_fields_map` defined in XML ([#921](https://github.com/OpenMage/magento-lts/pull/921))
- enabled website level config cache ([#2355](https://github.com/OpenMage/magento-lts/pull/2355))
- make overrides of Mage_Core_Model_Resource_Db_Abstract::delete respect parent api ([#1257](https://github.com/OpenMage/magento-lts/pull/1257))
- rewrite Mage_Eav_Model_Config as cache for all eav entity and attribute reads ([#2993](https://github.com/OpenMage/magento-lts/pull/2993))

For full list of changes, you can [compare tags](https://github.com/OpenMage/magento-lts/compare/1.9.4.x...20.0).

Expand Down Expand Up @@ -550,6 +551,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://github.com/discountscott"><img src="https://avatars.githubusercontent.com/u/5454596?v=4" loading="lazy" width="100" alt=""/><br /><sub><b>Scott Moore</b></sub></a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/rfeese"><img src="https://avatars.githubusercontent.com/u/7074181?v=4" loading="lazy" width="100" alt=""/><br /><sub><b>Roger Feese</b></sub></a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/AGelzer"><img src="https://avatars.githubusercontent.com/u/34437931?v=4" loading="lazy" width="100" alt=""/><br /><sub><b>Alexander Gelzer</b></sub></a></td>
<td align="center" valign="top" width="14.28%"><a href="https://gitlab.com/davidhiendl"><img src="https://avatars.githubusercontent.com/u/11006964?v=4" loading="lazy" width="100" alt=""/><br /><sub><b>David Hiendl</b></sub></a></td>
</tr>
</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ public function getIsCurrentSetDefault()
{
$isDefault = $this->getData('is_current_set_default');
if (is_null($isDefault)) {
$defaultSetId = Mage::getModel('eav/entity_type')
->load(Mage::registry('entityType'))
$defaultSetId = Mage::getSingleton('eav/config')
->getEntityType(Mage::registry('entityType'))
->getDefaultAttributeSetId();
$isDefault = $this->_getSetId() == $defaultSetId;
$this->setData('is_current_set_default', $isDefault);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ protected function _getSetId()
{
return ((int) $this->getRequest()->getParam('id') > 0)
? (int) $this->getRequest()->getParam('id')
: Mage::getModel('eav/entity_type')
->load(Mage::registry('entityType'))
: Mage::getSingleton('eav/config')->getEntityType(Mage::registry('entityType'))
->getDefaultAttributeSetId();
}
}
2 changes: 1 addition & 1 deletion app/code/core/Mage/Api2/Model/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ public function getEavAttributes($onlyVisible = false, $excludeSystem = false)
$model = $this->getConfig()->getResourceWorkingModel($this->getResourceType());

/** @var Mage_Eav_Model_Entity_Type $entityType */
$entityType = Mage::getModel('eav/entity_type')->load($model, 'entity_model');
$entityType = Mage::getSingleton('eav/config')->getEntityType($model, 'entity_model');

/** @var Mage_Eav_Model_Entity_Attribute $attribute */
foreach ($entityType->getAttributeCollection() as $attribute) {
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Catalog/Block/Product/Compare/List.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function getItems()
/**
* Retrieve Product Compare Attributes
*
* @return array
* @return Mage_Eav_Model_Entity_Attribute_Abstract[]
*/
public function getAttributes()
{
Expand Down
4 changes: 2 additions & 2 deletions app/code/core/Mage/Catalog/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ public function canApplyMsrpToProductType($product)
{
if ($this->_mapApplyToProductType === null) {
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $attribute */
$attribute = Mage::getModel('catalog/resource_eav_attribute')
->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'msrp_enabled');
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'msrp_enabled');
$this->_mapApplyToProductType = $attribute->getApplyTo();
}
return empty($this->_mapApplyToProductType) || in_array($product->getTypeId(), $this->_mapApplyToProductType);
Expand Down
2 changes: 1 addition & 1 deletion app/code/core/Mage/Catalog/Model/Api2/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getAvailableAttributes($userType, $operation)
{
$attributes = $this->getAvailableAttributesFromConfig();
/** @var Mage_Eav_Model_Entity_Type $entityType */
$entityType = Mage::getModel('eav/entity_type')->loadByCode('catalog_product');
$entityType = Mage::getSingleton('eav/config')->getEntityType(Mage_Catalog_Model_Product::ENTITY);
$entityOnlyAttrs = $this->getEntityOnlyAttributes($userType, $operation);
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $attribute */
foreach ($entityType->getAttributeCollection() as $attribute) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function isValidData(array $data)
try {
$this->_validateProductType($data);
/** @var Mage_Eav_Model_Entity_Type $productEntity */
$productEntity = Mage::getModel('eav/entity_type')->loadByCode(Mage_Catalog_Model_Product::ENTITY);
$productEntity = Mage::getSingleton('eav/config')->getEntityType(Mage_Catalog_Model_Product::ENTITY);
$this->_validateAttributeSet($data, $productEntity);
$this->_validateSku($data);
$this->_validateGiftOptions($data);
Expand Down
31 changes: 12 additions & 19 deletions app/code/core/Mage/Catalog/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,13 @@ protected function _getResource()
public function getAttributesUsedInProductListing()
{
if (is_null($this->_usedInProductListing)) {
$allAttributes = Mage::getSingleton('eav/config')
->getAttributes(Mage_Catalog_Model_Product::ENTITY);
$this->_usedInProductListing = [];
$entityType = Mage_Catalog_Model_Product::ENTITY;
$attributesData = $this->_getResource()
->setStoreId($this->getStoreId())
->getAttributesUsedInListing();
Mage::getSingleton('eav/config')
->importAttributesData($entityType, $attributesData);
foreach ($attributesData as $attributeData) {
$attributeCode = $attributeData['attribute_code'];
$this->_usedInProductListing[$attributeCode] = Mage::getSingleton('eav/config')
->getAttribute($entityType, $attributeCode);
foreach ($allAttributes as $attribute) {
if ($attribute->getData('used_in_product_listing')) {
$this->_usedInProductListing[$attribute->getAttributeCode()] = $attribute;
}
}
}
return $this->_usedInProductListing;
Expand All @@ -354,16 +350,13 @@ public function getAttributesUsedInProductListing()
public function getAttributesUsedForSortBy()
{
if (is_null($this->_usedForSortBy)) {
$allAttributes = Mage::getSingleton('eav/config')
->getAttributes(Mage_Catalog_Model_Product::ENTITY);
$this->_usedForSortBy = [];
$entityType = Mage_Catalog_Model_Product::ENTITY;
$attributesData = $this->_getResource()
->getAttributesUsedForSortBy();
Mage::getSingleton('eav/config')
->importAttributesData($entityType, $attributesData);
foreach ($attributesData as $attributeData) {
$attributeCode = $attributeData['attribute_code'];
$this->_usedForSortBy[$attributeCode] = Mage::getSingleton('eav/config')
->getAttribute($entityType, $attributeCode);
foreach ($allAttributes as $attribute) {
if ($attribute->getData('used_for_sort_by')) {
$this->_usedForSortBy[$attribute->getAttributeCode()] = $attribute;
}
}
}
return $this->_usedForSortBy;
Expand Down
31 changes: 20 additions & 11 deletions app/code/core/Mage/Catalog/Model/Layer.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,34 @@ public function getCurrentStore()
/**
* Get collection of all filterable attributes for layer products set
*
* @return Mage_Catalog_Model_Resource_Product_Attribute_Collection|array
* @return Mage_Catalog_Model_Resource_Eav_Attribute[]
*/
public function getFilterableAttributes()
{
$setIds = $this->_getSetIds();
if (!$setIds) {
return [];
}
/** @var Mage_Catalog_Model_Resource_Product_Attribute_Collection $collection */
$collection = Mage::getResourceModel('catalog/product_attribute_collection');
$collection
->setItemObjectClass('catalog/resource_eav_attribute')
->setAttributeSetFilter($setIds)
->addStoreLabel(Mage::app()->getStore()->getId())
->setOrder('position', 'ASC');
$collection = $this->_prepareAttributeCollection($collection);
$collection->load();

return $collection;
$eavConfig = Mage::getSingleton('eav/config');
/** @var Mage_Catalog_Model_Resource_Eav_Attribute[] $attributes */
$attributes = [];
foreach ($setIds as $setId) {
$setAttributeIds = $eavConfig->getAttributeSetAttributeIds($setId);
foreach ($setAttributeIds as $attributeId) {
if (!isset($attributes[$attributeId])) {
$attribute = $eavConfig->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeId);
if ($attribute instanceof Mage_Catalog_Model_Resource_Eav_Attribute && $attribute->getIsFilterable()) {
$attributes[$attributeId] = $attribute;
}
}
}
}
usort($attributes, function ($a, $b) {
return $a->getPosition() - $b->getPosition();
});

return $attributes;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
* Check whether specified attribute can be used in LN
*
* @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
* @return Mage_Catalog_Model_Resource_Eav_Attribute
* @return int
*/
protected function _getIsFilterableAttribute($attribute)
{
Expand Down
18 changes: 7 additions & 11 deletions app/code/core/Mage/Catalog/Model/Resource/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,17 +483,13 @@ public function getChildrenAmount($category, $isActiveFlag = true)
protected function _getIsActiveAttributeId()
{
if ($this->_isActiveAttributeId === null) {
$bind = [
'catalog_category' => Mage_Catalog_Model_Category::ENTITY,
'is_active' => 'is_active',
];
$select = $this->_getReadAdapter()->select()
->from(['a' => $this->getTable('eav/attribute')], ['attribute_id'])
->join(['t' => $this->getTable('eav/entity_type')], 'a.entity_type_id = t.entity_type_id')
->where('entity_type_code = :catalog_category')
->where('attribute_code = :is_active');

$this->_isActiveAttributeId = $this->_getReadAdapter()->fetchOne($select, $bind);
$attributeId = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Category::ENTITY, 'is_active')
->getId();
if (!is_int($attributeId)) {
Mage::throwException("Failed to find category attribute is_active");
}
$this->_isActiveAttributeId = $attributeId;
}

return $this->_isActiveAttributeId;
Expand Down
10 changes: 9 additions & 1 deletion app/code/core/Mage/Catalog/Model/Resource/Eav/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
* @method $this setIsSearchable(int $value)
* @method int getSearchWeight()
* @method $this setSearchWeight(int $value)
* @method int getIsFilterable()
* @method $this setIsFilterable(int $value)
* @method int getIsComparable()
* @method $this setIsComparable(int $value)
Expand Down Expand Up @@ -288,6 +287,15 @@ public function getFrontendLabel()
return $this->_getData('frontend_label');
}

/**
* Retrieve is_filterable value
* @return int
*/
public function getIsFilterable()
{
return $this->_getData('is_filterable');
}

/**
* Get Attribute translated label for store
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Mage_Catalog_Model_Resource_Product_Compare_Item_Collection extends Mage_C
/**
* Comparable attributes cache
*
* @var array|null
* @var Mage_Eav_Model_Entity_Attribute_Abstract[]|null
*/
protected $_comparableAttributes;

Expand Down Expand Up @@ -211,44 +211,27 @@ protected function _getAttributeIdsBySetIds(array $setIds)
/**
* Retrieve Merged comparable attributes for compared product items
*
* @return array
* @return Mage_Eav_Model_Entity_Attribute_Abstract[]
*/
public function getComparableAttributes()
{
if (is_null($this->_comparableAttributes)) {
$this->_comparableAttributes = [];
$setIds = $this->_getAttributeSetIds();
if ($setIds) {
$select = $this->getConnection()->select()
->from(['main_table' => $this->getTable('eav/attribute')])
->join(
['additional_table' => $this->getTable('catalog/eav_attribute')],
'additional_table.attribute_id=main_table.attribute_id'
)
->joinLeft(
['al' => $this->getTable('eav/attribute_label')],
'al.attribute_id = main_table.attribute_id AND al.store_id = ' . (int) $this->getStoreId(),
['store_label' => new Zend_Db_Expr('IFNULL(al.value, main_table.frontend_label)')]
)
->joinLeft(
['ai' => $this->getTable('eav/entity_attribute')],
'ai.attribute_id = main_table.attribute_id'
)
->where('additional_table.is_comparable=?', 1)
->where('ai.attribute_set_id IN(?)', $setIds)
->order(['ai.attribute_group_id ASC', 'ai.sort_order ASC']);
$attributesData = $this->getConnection()->fetchAll($select);
if ($attributesData) {
$entityType = Mage_Catalog_Model_Product::ENTITY;
Mage::getSingleton('eav/config')
->importAttributesData($entityType, $attributesData);
foreach ($attributesData as $data) {
$attribute = Mage::getSingleton('eav/config')
->getAttribute($entityType, $data['attribute_code']);
$eavConfig = Mage::getSingleton('eav/config');
$attributeIds = $eavConfig->getAttributeSetAttributeIds($setIds);
$this->_comparableAttributes = [];
foreach ($attributeIds as $attributeId) {
$attribute = $eavConfig->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeId);
if ($attribute->getData('is_comparable')) {
$this->_comparableAttributes[$attribute->getAttributeCode()] = $attribute;
}
unset($attributesData);
}

usort($this->_comparableAttributes, function ($a, $b) {
return $a->getPosition() - $b->getPosition();
});
}
}
return $this->_comparableAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ public function getAttributeCodes()

$select->where(implode(' OR ', $whereCondition));
$attributesData = $adapter->fetchAll($select, $bind);
Mage::getSingleton('eav/config')
->importAttributesData($this->getEntityType(), $attributesData);

foreach ($attributesData as $data) {
$this->_attributeCodes[$data['attribute_id']] = $data['attribute_code'];
Expand Down Expand Up @@ -264,8 +262,8 @@ public function getAttribute($attributeCode)
{
$attributes = $this->getAttributes();
if (!isset($attributes[$attributeCode])) {
$attribute = Mage::getModel('catalog/resource_eav_attribute')
->loadByCode($this->getEntityTypeId(), $attributeCode);
$attribute = Mage::getSingleton('eav/config')
->getAttribute($this->getEntityTypeId(), $attributeCode);
if (!$attribute->getId()) {
Mage::throwException(Mage::helper('catalog')->__('Invalid attribute %s', $attributeCode));
}
Expand All @@ -274,6 +272,10 @@ public function getAttribute($attributeCode)
->getEntity();
$attribute->setEntity($entity);

if (!($attribute instanceof Mage_Eav_Model_Entity_Attribute)) {
throw new Exception('Product attribute(code=' . $attributeCode . ') is expected to be of type Mage_Eav_Model_Entity_Attribute');
}

return $attribute;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function getTierPriceAttribute()
{
$data = $this->getData('tier_price_attribute');
if (is_null($data)) {
$data = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'tier_price');
$data = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'tier_price');
$this->setData('tier_price_attribute', $data);
}
return $data;
Expand All @@ -79,7 +79,7 @@ public function getPriceAttribute()
{
$data = $this->getData('price_attribute');
if (is_null($data)) {
$data = Mage::getModel('eav/entity_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'price');
$data = Mage::getSingleton('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'price');
$this->setData('price_attribute', $data);
}
return $data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Mage_CatalogSearch_Model_Layer_Filter_Attribute extends Mage_Catalog_Model
* Check whether specified attribute can be used in LN
*
* @param Mage_Catalog_Model_Resource_Eav_Attribute $attribute
* @return bool
* @return int
*/
protected function _getIsFilterableAttribute($attribute)
{
Expand Down
Loading

0 comments on commit fc97382

Please sign in to comment.