AbstractAdapterTrait.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. } catch (\Exception $e) {
  46. CacheItem::log($this->logger, 'Failed to fetch key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e]);
  47. }
  48. return $f($key, $value, $isHit);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getItems(array $keys = [])
  54. {
  55. if ($this->deferred) {
  56. $this->commit();
  57. }
  58. $ids = [];
  59. foreach ($keys as $key) {
  60. $ids[] = $this->getId($key);
  61. }
  62. try {
  63. $items = $this->doFetch($ids);
  64. } catch (\Exception $e) {
  65. CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => $keys, 'exception' => $e]);
  66. $items = [];
  67. }
  68. $ids = array_combine($ids, $keys);
  69. return $this->generateItems($items, $ids);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function save(CacheItemInterface $item)
  75. {
  76. if (!$item instanceof CacheItem) {
  77. return false;
  78. }
  79. $this->deferred[$item->getKey()] = $item;
  80. return $this->commit();
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function saveDeferred(CacheItemInterface $item)
  86. {
  87. if (!$item instanceof CacheItem) {
  88. return false;
  89. }
  90. $this->deferred[$item->getKey()] = $item;
  91. return true;
  92. }
  93. public function __destruct()
  94. {
  95. if ($this->deferred) {
  96. $this->commit();
  97. }
  98. }
  99. private function generateItems($items, &$keys)
  100. {
  101. $f = $this->createCacheItem;
  102. try {
  103. foreach ($items as $id => $value) {
  104. if (!isset($keys[$id])) {
  105. $id = key($keys);
  106. }
  107. $key = $keys[$id];
  108. unset($keys[$id]);
  109. yield $key => $f($key, $value, true);
  110. }
  111. } catch (\Exception $e) {
  112. CacheItem::log($this->logger, 'Failed to fetch items: '.$e->getMessage(), ['keys' => array_values($keys), 'exception' => $e]);
  113. }
  114. foreach ($keys as $key) {
  115. yield $key => $f($key, null, false);
  116. }
  117. }
  118. }