Skip to content

Commit

Permalink
MAGETWO-65484: [Backport] - [Performance] Caching of attribute option…
Browse files Browse the repository at this point in the history
…s for Layered Navigation - for 2.1.6
  • Loading branch information
vzabaznov committed Mar 7, 2017
1 parent 54c2a0e commit 67b5ff7
Showing 1 changed file with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 67b5ff7

Please sign in to comment.