AbstractAdapterTrait.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. *
  17. * @internal
  18. */
  19. trait AbstractAdapterTrait
  20. {
  21. use AbstractTrait;
  22. /**
  23. * @var \Closure needs to be set by class, signature is function(string <key>, mixed <value>, bool <isHit>)
  24. */
  25. private $createCacheItem;
  26. /**
  27. * @var \Closure needs to be set by class, signature is function(array <deferred>, string <namespace>, array <&expiredIds>)
  28. */
  29. private $mergeByLifetime;
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getItem($key)
  34. {
  35. $id = $this->getId($key);
  36. if (isset($this->deferred[$key])) {
  37. $this->commit();
  38. }
  39. $f = $this->createCacheItem;
  40. $isHit = false;
  41. $value = null;
  42. try {
  43. foreach ($this->doFetch([$id]) as $value) {
  44. $isHit = true;
  45. }
  46. return $f($key, $value, $isHit);
  47. } catch (\Exception $e) {
  48. CacheItem::log($this->logger, 'Failed to fetch key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e]);
  49. }
  50. return $f($key, null, false);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getItems(array $keys = [])
  56. {
  57. $ids = [];
  58. $commit = false;
  59. foreach ($keys as $key) {
  60. $ids[] = $this->getId($key);
  61. $commit = $commit || isset($this->deferred[$key]);
  62. }
  63. if ($commit) {
  64. $this->commit();
  65. }
  66. try {
  67. $items = $this->doFetch($ids);
  68. } catch (\Exception $e) {
  69. CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => $keys, 'exception' => $e]);
  70. $items = [];
  71. }
  72. $ids = array_combine($ids, $keys);
  73. return $this->generateItems($items, $ids);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. *
  78. * @return bool
  79. */
  80. public function save(CacheItemInterface $item)
  81. {
  82. if (!$item instanceof CacheItem) {
  83. return false;
  84. }
  85. $this->deferred[$item->getKey()] = $item;
  86. return $this->commit();
  87. }
  88. /**
  89. * {@inheritdoc}
  90. *
  91. * @return bool
  92. */
  93. public function saveDeferred(CacheItemInterface $item)
  94. {
  95. if (!$item instanceof CacheItem) {
  96. return false;
  97. }
  98. $this->deferred[$item->getKey()] = $item;
  99. return true;
  100. }
  101. /**
  102. * @return array
  103. */
  104. public function __sleep()
  105. {
  106. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  107. }
  108. public function __wakeup()
  109. {
  110. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  111. }
  112. public function __destruct()
  113. {
  114. if ($this->deferred) {
  115. $this->commit();
  116. }
  117. }
  118. private function generateItems(iterable $items, array &$keys): iterable
  119. {
  120. $f = $this->createCacheItem;
  121. try {
  122. foreach ($items as $id => $value) {
  123. if (!isset($keys[$id])) {
  124. throw new InvalidArgumentException(sprintf('Could not match value id "%s" to keys "%s".', $id, implode('", "', $keys)));
  125. }
  126. $key = $keys[$id];
  127. unset($keys[$id]);
  128. yield $key => $f($key, $value, true);
  129. }
  130. } catch (\Exception $e) {
  131. CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => array_values($keys), 'exception' => $e]);
  132. }
  133. foreach ($keys as $key) {
  134. yield $key => $f($key, null, false);
  135. }
  136. }
  137. }