From 67b5ff743a11449fe97d0fcf711be30df6ec878b Mon Sep 17 00:00:00 2001 From: vzabaznov Date: Tue, 7 Mar 2017 16:17:38 +0200 Subject: [PATCH] MAGETWO-65484: [Backport] - [Performance] Caching of attribute options for Layered Navigation - for 2.1.6 --- .../Attribute/Frontend/AbstractFrontend.php | 54 +++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php b/app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php index ca2520c621a3a..5979526457cf7 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php @@ -11,8 +11,29 @@ */ namespace Magento\Eav\Model\Entity\Attribute\Frontend; +use Magento\Framework\App\CacheInterface; +use Magento\Store\Api\StoreResolverInterface; +use Magento\Framework\App\ObjectManager; +use Magento\Eav\Model\Cache\Type as CacheType; +use Magento\Eav\Model\Entity\Attribute; + abstract class AbstractFrontend implements \Magento\Eav\Model\Entity\Attribute\Frontend\FrontendInterface { + /** + * @var CacheInterface + */ + private $cache; + + /** + * @var StoreResolverInterface + */ + private $storeResolver; + + /** + * @var array + */ + private $cacheTags; + /** * Reference to the attribute instance * @@ -27,11 +48,24 @@ abstract class AbstractFrontend implements \Magento\Eav\Model\Entity\Attribute\F /** * @param \Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory $attrBooleanFactory + * @param CacheInterface $cache + * @param StoreResolverInterface $storeResolver + * @param array $cacheTags * @codeCoverageIgnore */ - public function __construct(\Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory $attrBooleanFactory) - { + public function __construct( + \Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory $attrBooleanFactory, + CacheInterface $cache = null, + StoreResolverInterface $storeResolver = null, + array $cacheTags = [ + CacheType::CACHE_TAG, + Attribute::CACHE_TAG, + ] + ) { $this->_attrBooleanFactory = $attrBooleanFactory; + $this->cache = $cache ?: ObjectManager::getInstance()->get(CacheInterface::class); + $this->storeResolver = $storeResolver ?: ObjectManager::getInstance()->get(StoreResolverInterface::class); + $this->cacheTags = $cacheTags; } /** @@ -216,7 +250,21 @@ public function getConfigField($fieldName) */ public function getSelectOptions() { - return $this->getAttribute()->getSource()->getAllOptions(); + $cacheKey = 'attribute-navigation-option-' . + $this->getAttribute()->getAttributeCode() . '-' . + $this->storeResolver->getCurrentStoreId(); + $optionString = $this->cache->load($cacheKey); + if (false === $optionString) { + $options = $this->getAttribute()->getSource()->getAllOptions(); + $this->cache->save( + json_encode($options), + $cacheKey, + $this->cacheTags + ); + } else { + $options = json_decode($optionString, true); + } + return $options; } /**