NullAdapter.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. use Symfony\Contracts\Cache\NamespacedPoolInterface;
  15. /**
  16. * @author Titouan Galopin <galopintitouan@gmail.com>
  17. */
  18. class NullAdapter implements AdapterInterface, CacheInterface, NamespacedPoolInterface
  19. {
  20. private static \Closure $createCacheItem;
  21. public function __construct()
  22. {
  23. self::$createCacheItem ??= \Closure::bind(
  24. static function ($key) {
  25. $item = new CacheItem();
  26. $item->key = $key;
  27. $item->isHit = false;
  28. return $item;
  29. },
  30. null,
  31. CacheItem::class
  32. );
  33. }
  34. public function get(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null): mixed
  35. {
  36. $save = true;
  37. return $callback((self::$createCacheItem)($key), $save);
  38. }
  39. public function getItem(mixed $key): CacheItem
  40. {
  41. return (self::$createCacheItem)($key);
  42. }
  43. public function getItems(array $keys = []): iterable
  44. {
  45. return $this->generateItems($keys);
  46. }
  47. public function hasItem(mixed $key): bool
  48. {
  49. return false;
  50. }
  51. public function clear(string $prefix = ''): bool
  52. {
  53. return true;
  54. }
  55. public function deleteItem(mixed $key): bool
  56. {
  57. return true;
  58. }
  59. public function deleteItems(array $keys): bool
  60. {
  61. return true;
  62. }
  63. public function save(CacheItemInterface $item): bool
  64. {
  65. return true;
  66. }
  67. public function saveDeferred(CacheItemInterface $item): bool
  68. {
  69. return true;
  70. }
  71. public function commit(): bool
  72. {
  73. return true;
  74. }
  75. public function delete(string $key): bool
  76. {
  77. return $this->deleteItem($key);
  78. }
  79. public function withSubNamespace(string $namespace): static
  80. {
  81. return clone $this;
  82. }
  83. private function generateItems(array $keys): \Generator
  84. {
  85. $f = self::$createCacheItem;
  86. foreach ($keys as $key) {
  87. yield $key => $f($key);
  88. }
  89. }
  90. }