NullAdapter.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Adapter;
  11. use Psr\Cache\CacheItemInterface;
  12. use Symfony\Component\Cache\CacheItem;
  13. use Symfony\Contracts\Cache\CacheInterface;
  14. /**
  15. * @author Titouan Galopin <galopintitouan@gmail.com>
  16. */
  17. class NullAdapter implements AdapterInterface, CacheInterface
  18. {
  19. private $createCacheItem;
  20. public function __construct()
  21. {
  22. $this->createCacheItem = \Closure::bind(
  23. function ($key) {
  24. $item = new CacheItem();
  25. $item->key = $key;
  26. $item->isHit = false;
  27. return $item;
  28. },
  29. $this,
  30. CacheItem::class
  31. );
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
  37. {
  38. $save = true;
  39. return $callback(($this->createCacheItem)($key), $save);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getItem($key)
  45. {
  46. $f = $this->createCacheItem;
  47. return $f($key);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getItems(array $keys = [])
  53. {
  54. return $this->generateItems($keys);
  55. }
  56. /**
  57. * {@inheritdoc}
  58. *
  59. * @return bool
  60. */
  61. public function hasItem($key)
  62. {
  63. return false;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. *
  68. * @param string $prefix
  69. *
  70. * @return bool
  71. */
  72. public function clear(/*string $prefix = ''*/)
  73. {
  74. return true;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. *
  79. * @return bool
  80. */
  81. public function deleteItem($key)
  82. {
  83. return true;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. *
  88. * @return bool
  89. */
  90. public function deleteItems(array $keys)
  91. {
  92. return true;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. *
  97. * @return bool
  98. */
  99. public function save(CacheItemInterface $item)
  100. {
  101. return false;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. *
  106. * @return bool
  107. */
  108. public function saveDeferred(CacheItemInterface $item)
  109. {
  110. return false;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. *
  115. * @return bool
  116. */
  117. public function commit()
  118. {
  119. return false;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function delete(string $key): bool
  125. {
  126. return $this->deleteItem($key);
  127. }
  128. private function generateItems(array $keys)
  129. {
  130. $f = $this->createCacheItem;
  131. foreach ($keys as $key) {
  132. yield $key => $f($key);
  133. }
  134. }
  135. }