Cache.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Collection\Db\FetchStrategy;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\DB\Select;
  9. use Magento\Framework\Serialize\SerializerInterface;
  10. /**
  11. * Retrieve collection data from cache, fail over to another fetch strategy, if cache does not exist yet
  12. */
  13. class Cache implements \Magento\Framework\Data\Collection\Db\FetchStrategyInterface
  14. {
  15. /**
  16. * @var \Magento\Framework\Cache\FrontendInterface
  17. */
  18. private $_cache;
  19. /**
  20. * @var \Magento\Framework\Data\Collection\Db\FetchStrategyInterface
  21. */
  22. private $_fetchStrategy;
  23. /**
  24. * @var string
  25. */
  26. protected $_cacheIdPrefix = '';
  27. /**
  28. * @var array
  29. */
  30. protected $_cacheTags = [];
  31. /**
  32. * @var int|bool|null
  33. */
  34. protected $_cacheLifetime = null;
  35. /**
  36. * @var SerializerInterface
  37. */
  38. private $serializer;
  39. /**
  40. * Constructor
  41. *
  42. * @param \Magento\Framework\Cache\FrontendInterface $cache
  43. * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
  44. * @param string $cacheIdPrefix
  45. * @param array $cacheTags
  46. * @param int|bool|null $cacheLifetime
  47. * @param SerializerInterface|null $serializer
  48. */
  49. public function __construct(
  50. \Magento\Framework\Cache\FrontendInterface $cache,
  51. \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
  52. $cacheIdPrefix = '',
  53. array $cacheTags = [],
  54. $cacheLifetime = null,
  55. SerializerInterface $serializer = null
  56. ) {
  57. $this->_cache = $cache;
  58. $this->_fetchStrategy = $fetchStrategy;
  59. $this->_cacheIdPrefix = $cacheIdPrefix;
  60. $this->_cacheTags = $cacheTags;
  61. $this->_cacheLifetime = $cacheLifetime;
  62. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function fetchAll(Select $select, array $bindParams = [])
  68. {
  69. $cacheId = $this->_getSelectCacheId($select);
  70. $result = $this->_cache->load($cacheId);
  71. if ($result) {
  72. $result = $this->serializer->unserialize($result);
  73. } else {
  74. $result = $this->_fetchStrategy->fetchAll($select, $bindParams);
  75. $this->_cache->save(
  76. $this->serializer->serialize($result),
  77. $cacheId,
  78. $this->_cacheTags,
  79. $this->_cacheLifetime
  80. );
  81. }
  82. return $result;
  83. }
  84. /**
  85. * Determine cache identifier based on select query
  86. *
  87. * @param \Magento\Framework\DB\Select|string $select
  88. * @return string
  89. */
  90. protected function _getSelectCacheId($select)
  91. {
  92. return $this->_cacheIdPrefix . md5((string)$select);
  93. }
  94. }