AbstractAdapterTrait.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Traits;
  11. use Psr\Cache\CacheItemInterface;
  12. use Symfony\Component\Cache\CacheItem;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. *
  16. * @internal
  17. */
  18. trait AbstractAdapterTrait
  19. {
  20. use AbstractTrait;
  21. /**
  22. * @var \Closure needs to be set by class, signature is function(string <key>, mixed <value>, bool <isHit>)
  23. */
  24. private $createCacheItem;
  25. /**
  26. * @var \Closure needs to be set by class, signature is function(array <deferred>, string <namespace>, array <&expiredIds>)
  27. */
  28. private $mergeByLifetime;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getItem($key)
  33. {
  34. if ($this->deferred) {
  35. $this->commit();
  36. }
  37. $id = $this->getId($key);
  38. $f = $this->createCacheItem;
  39. $isHit = false;
  40. $value = null;
  41. try {
  42. foreach ($this->doFetch([$id]) as $value) {
  43. $isHit = true;
  44. }
  45. return $f($key, $value, $isHit);
  46. } catch (\Exception $e) {
  47. CacheItem::log($this->logger, 'Failed to fetch key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e]);
  48. }
  49. return $f($key, null, false);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function getItems(array $keys = [])
  55. {
  56. if ($this->deferred) {
  57. $this->commit();
  58. }
  59. $ids = [];
  60. foreach ($keys as $key) {
  61. $ids[] = $this->getId($key);
  62. }
  63. try {
  64. $items = $this->doFetch($ids);
  65. } catch (\Exception $e) {
  66. CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => $keys, 'exception' => $e]);
  67. $items = [];
  68. }
  69. $ids = array_combine($ids, $keys);
  70. return $this->generateItems($items, $ids);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. *
  75. * @return bool
  76. */
  77. public function save(CacheItemInterface $item)
  78. {
  79. if (!$item instanceof CacheItem) {
  80. return false;
  81. }
  82. $this->deferred[$item->getKey()] = $item;
  83. return $this->commit();
  84. }
  85. /**
  86. * {@inheritdoc}
  87. *
  88. * @return bool
  89. */
  90. public function saveDeferred(CacheItemInterface $item)
  91. {
  92. if (!$item instanceof CacheItem) {
  93. return false;
  94. }
  95. $this->deferred[$item->getKey()] = $item;
  96. return true;
  97. }
  98. public function __sleep()
  99. {
  100. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  101. }
  102. public function __wakeup()
  103. {
  104. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  105. }
  106. public function __destruct()
  107. {
  108. if ($this->deferred) {
  109. $this->commit();
  110. }
  111. }
  112. private function generateItems(iterable $items, array &$keys): iterable
  113. {
  114. $f = $this->createCacheItem;
  115. try {
  116. foreach ($items as $id => $value) {
  117. if (!isset($keys[$id])) {
  118. $id = key($keys);
  119. }
  120. $key = $keys[$id];
  121. unset($keys[$id]);
  122. yield $key => $f($key, $value, true);
  123. }
  124. } catch (\Exception $e) {
  125. CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => array_values($keys), 'exception' => $e]);
  126. }
  127. foreach ($keys as $key) {
  128. yield $key => $f($key, null, false);
  129. }
  130. }
  131. }