Skip to content

Commit 67b5ff7

Browse files
committed
MAGETWO-65484: [Backport] - [Performance] Caching of attribute options for Layered Navigation - for 2.1.6
1 parent 54c2a0e commit 67b5ff7

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

app/code/Magento/Eav/Model/Entity/Attribute/Frontend/AbstractFrontend.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,29 @@
1111
*/
1212
namespace Magento\Eav\Model\Entity\Attribute\Frontend;
1313

14+
use Magento\Framework\App\CacheInterface;
15+
use Magento\Store\Api\StoreResolverInterface;
16+
use Magento\Framework\App\ObjectManager;
17+
use Magento\Eav\Model\Cache\Type as CacheType;
18+
use Magento\Eav\Model\Entity\Attribute;
19+
1420
abstract class AbstractFrontend implements \Magento\Eav\Model\Entity\Attribute\Frontend\FrontendInterface
1521
{
22+
/**
23+
* @var CacheInterface
24+
*/
25+
private $cache;
26+
27+
/**
28+
* @var StoreResolverInterface
29+
*/
30+
private $storeResolver;
31+
32+
/**
33+
* @var array
34+
*/
35+
private $cacheTags;
36+
1637
/**
1738
* Reference to the attribute instance
1839
*
@@ -27,11 +48,24 @@ abstract class AbstractFrontend implements \Magento\Eav\Model\Entity\Attribute\F
2748

2849
/**
2950
* @param \Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory $attrBooleanFactory
51+
* @param CacheInterface $cache
52+
* @param StoreResolverInterface $storeResolver
53+
* @param array $cacheTags
3054
* @codeCoverageIgnore
3155
*/
32-
public function __construct(\Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory $attrBooleanFactory)
33-
{
56+
public function __construct(
57+
\Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory $attrBooleanFactory,
58+
CacheInterface $cache = null,
59+
StoreResolverInterface $storeResolver = null,
60+
array $cacheTags = [
61+
CacheType::CACHE_TAG,
62+
Attribute::CACHE_TAG,
63+
]
64+
) {
3465
$this->_attrBooleanFactory = $attrBooleanFactory;
66+
$this->cache = $cache ?: ObjectManager::getInstance()->get(CacheInterface::class);
67+
$this->storeResolver = $storeResolver ?: ObjectManager::getInstance()->get(StoreResolverInterface::class);
68+
$this->cacheTags = $cacheTags;
3569
}
3670

3771
/**
@@ -216,7 +250,21 @@ public function getConfigField($fieldName)
216250
*/
217251
public function getSelectOptions()
218252
{
219-
return $this->getAttribute()->getSource()->getAllOptions();
253+
$cacheKey = 'attribute-navigation-option-' .
254+
$this->getAttribute()->getAttributeCode() . '-' .
255+
$this->storeResolver->getCurrentStoreId();
256+
$optionString = $this->cache->load($cacheKey);
257+
if (false === $optionString) {
258+
$options = $this->getAttribute()->getSource()->getAllOptions();
259+
$this->cache->save(
260+
json_encode($options),
261+
$cacheKey,
262+
$this->cacheTags
263+
);
264+
} else {
265+
$options = json_decode($optionString, true);
266+
}
267+
return $options;
220268
}
221269

222270
/**

0 commit comments

Comments
 (0)