Config.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Plugin\Model\ResourceModel;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. /**
  10. * Config cache plugin.
  11. */
  12. class Config
  13. {
  14. /**#@+
  15. * Product listing attributes cache ids
  16. */
  17. const PRODUCT_LISTING_ATTRIBUTES_CACHE_ID = 'PRODUCT_LISTING_ATTRIBUTES';
  18. const PRODUCT_LISTING_SORT_BY_ATTRIBUTES_CACHE_ID = 'PRODUCT_LISTING_SORT_BY_ATTRIBUTES';
  19. /**#@-*/
  20. /**#@-*/
  21. protected $cache;
  22. /**
  23. * @var bool|null
  24. */
  25. protected $isCacheEnabled = null;
  26. /**
  27. * @var SerializerInterface
  28. */
  29. private $serializer;
  30. /**
  31. * @param \Magento\Framework\App\CacheInterface $cache
  32. * @param \Magento\Framework\App\Cache\StateInterface $cacheState
  33. * @param SerializerInterface $serializer
  34. */
  35. public function __construct(
  36. \Magento\Framework\App\CacheInterface $cache,
  37. \Magento\Framework\App\Cache\StateInterface $cacheState,
  38. SerializerInterface $serializer = null
  39. ) {
  40. $this->cache = $cache;
  41. $this->isCacheEnabled = $cacheState->isEnabled(\Magento\Eav\Model\Cache\Type::TYPE_IDENTIFIER);
  42. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  43. }
  44. /**
  45. * Cache attribute used in listing.
  46. *
  47. * @param \Magento\Catalog\Model\ResourceModel\Config $config
  48. * @param \Closure $proceed
  49. * @return array
  50. */
  51. public function aroundGetAttributesUsedInListing(
  52. \Magento\Catalog\Model\ResourceModel\Config $config,
  53. \Closure $proceed
  54. ) {
  55. $cacheId = self::PRODUCT_LISTING_ATTRIBUTES_CACHE_ID . $config->getEntityTypeId() . '_' . $config->getStoreId();
  56. if ($this->isCacheEnabled && ($attributes = $this->cache->load($cacheId))) {
  57. return $this->serializer->unserialize($attributes);
  58. }
  59. $attributes = $proceed();
  60. if ($this->isCacheEnabled) {
  61. $this->cache->save(
  62. $this->serializer->serialize($attributes),
  63. $cacheId,
  64. [
  65. \Magento\Eav\Model\Cache\Type::CACHE_TAG,
  66. \Magento\Eav\Model\Entity\Attribute::CACHE_TAG
  67. ]
  68. );
  69. }
  70. return $attributes;
  71. }
  72. /**
  73. * Cache attributes used for sorting.
  74. *
  75. * @param \Magento\Catalog\Model\ResourceModel\Config $config
  76. * @param \Closure $proceed
  77. * @return array
  78. */
  79. public function aroundGetAttributesUsedForSortBy(
  80. \Magento\Catalog\Model\ResourceModel\Config $config,
  81. \Closure $proceed
  82. ) {
  83. $cacheId = self::PRODUCT_LISTING_SORT_BY_ATTRIBUTES_CACHE_ID . $config->getEntityTypeId() . '_'
  84. . $config->getStoreId();
  85. if ($this->isCacheEnabled && ($attributes = $this->cache->load($cacheId))) {
  86. return $this->serializer->unserialize($attributes);
  87. }
  88. $attributes = $proceed();
  89. if ($this->isCacheEnabled) {
  90. $this->cache->save(
  91. $this->serializer->serialize($attributes),
  92. $cacheId,
  93. [
  94. \Magento\Eav\Model\Cache\Type::CACHE_TAG,
  95. \Magento\Eav\Model\Entity\Attribute::CACHE_TAG
  96. ]
  97. );
  98. }
  99. return $attributes;
  100. }
  101. }