ProxyAdapter.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 Psr\Cache\CacheItemPoolInterface;
  13. use Symfony\Component\Cache\CacheItem;
  14. use Symfony\Component\Cache\PruneableInterface;
  15. use Symfony\Component\Cache\ResettableInterface;
  16. use Symfony\Component\Cache\Traits\ContractsTrait;
  17. use Symfony\Component\Cache\Traits\ProxyTrait;
  18. use Symfony\Contracts\Cache\CacheInterface;
  19. use Symfony\Contracts\Cache\NamespacedPoolInterface;
  20. /**
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. */
  23. class ProxyAdapter implements AdapterInterface, NamespacedPoolInterface, CacheInterface, PruneableInterface, ResettableInterface
  24. {
  25. use ContractsTrait;
  26. use ProxyTrait;
  27. private string $namespace = '';
  28. private int $namespaceLen;
  29. private string $poolHash;
  30. private int $defaultLifetime;
  31. private static \Closure $createCacheItem;
  32. private static \Closure $setInnerItem;
  33. public function __construct(CacheItemPoolInterface $pool, string $namespace = '', int $defaultLifetime = 0)
  34. {
  35. if ('' !== $namespace) {
  36. if ($pool instanceof NamespacedPoolInterface) {
  37. $pool = $pool->withSubNamespace($namespace);
  38. $this->namespace = $namespace = '';
  39. } else {
  40. \assert('' !== CacheItem::validateKey($namespace));
  41. $this->namespace = $namespace;
  42. }
  43. }
  44. $this->pool = $pool;
  45. $this->poolHash = spl_object_hash($pool);
  46. $this->namespaceLen = \strlen($namespace);
  47. $this->defaultLifetime = $defaultLifetime;
  48. self::$createCacheItem ??= \Closure::bind(
  49. static function ($key, $innerItem, $poolHash) {
  50. $item = new CacheItem();
  51. $item->key = $key;
  52. if (null === $innerItem) {
  53. return $item;
  54. }
  55. $item->value = $innerItem->get();
  56. $item->isHit = $innerItem->isHit();
  57. $item->innerItem = $innerItem;
  58. $item->poolHash = $poolHash;
  59. if (!$item->unpack() && $innerItem instanceof CacheItem) {
  60. $item->metadata = $innerItem->metadata;
  61. }
  62. $innerItem->set(null);
  63. return $item;
  64. },
  65. null,
  66. CacheItem::class
  67. );
  68. self::$setInnerItem ??= \Closure::bind(
  69. static function (CacheItemInterface $innerItem, CacheItem $item, $expiry = null) {
  70. $innerItem->set($item->pack());
  71. $innerItem->expiresAt(($expiry ?? $item->expiry) ? \DateTimeImmutable::createFromFormat('U.u', \sprintf('%.6F', $expiry ?? $item->expiry)) : null);
  72. },
  73. null,
  74. CacheItem::class
  75. );
  76. }
  77. public function get(string $key, callable $callback, ?float $beta = null, ?array &$metadata = null): mixed
  78. {
  79. if (!$this->pool instanceof CacheInterface) {
  80. return $this->doGet($this, $key, $callback, $beta, $metadata);
  81. }
  82. return $this->pool->get($this->getId($key), function ($innerItem, bool &$save) use ($key, $callback) {
  83. $item = (self::$createCacheItem)($key, $innerItem, $this->poolHash);
  84. $item->set($value = $callback($item, $save));
  85. (self::$setInnerItem)($innerItem, $item);
  86. return $value;
  87. }, $beta, $metadata);
  88. }
  89. public function getItem(mixed $key): CacheItem
  90. {
  91. $item = $this->pool->getItem($this->getId($key));
  92. return (self::$createCacheItem)($key, $item, $this->poolHash);
  93. }
  94. public function getItems(array $keys = []): iterable
  95. {
  96. if ($this->namespaceLen) {
  97. foreach ($keys as $i => $key) {
  98. $keys[$i] = $this->getId($key);
  99. }
  100. }
  101. return $this->generateItems($this->pool->getItems($keys));
  102. }
  103. public function hasItem(mixed $key): bool
  104. {
  105. return $this->pool->hasItem($this->getId($key));
  106. }
  107. public function clear(string $prefix = ''): bool
  108. {
  109. if ($this->pool instanceof AdapterInterface) {
  110. return $this->pool->clear($this->namespace.$prefix);
  111. }
  112. return $this->pool->clear();
  113. }
  114. public function deleteItem(mixed $key): bool
  115. {
  116. return $this->pool->deleteItem($this->getId($key));
  117. }
  118. public function deleteItems(array $keys): bool
  119. {
  120. if ($this->namespaceLen) {
  121. foreach ($keys as $i => $key) {
  122. $keys[$i] = $this->getId($key);
  123. }
  124. }
  125. return $this->pool->deleteItems($keys);
  126. }
  127. public function save(CacheItemInterface $item): bool
  128. {
  129. return $this->doSave($item, __FUNCTION__);
  130. }
  131. public function saveDeferred(CacheItemInterface $item): bool
  132. {
  133. return $this->doSave($item, __FUNCTION__);
  134. }
  135. public function commit(): bool
  136. {
  137. return $this->pool->commit();
  138. }
  139. public function withSubNamespace(string $namespace): static
  140. {
  141. $clone = clone $this;
  142. if ($clone->pool instanceof NamespacedPoolInterface) {
  143. $clone->pool = $clone->pool->withSubNamespace($namespace);
  144. } else {
  145. $clone->namespace .= CacheItem::validateKey($namespace);
  146. $clone->namespaceLen = \strlen($clone->namespace);
  147. }
  148. return $clone;
  149. }
  150. private function doSave(CacheItemInterface $item, string $method): bool
  151. {
  152. if (!$item instanceof CacheItem) {
  153. return false;
  154. }
  155. $castItem = (array) $item;
  156. if (null === $castItem["\0*\0expiry"] && 0 < $this->defaultLifetime) {
  157. $castItem["\0*\0expiry"] = microtime(true) + $this->defaultLifetime;
  158. }
  159. if ($castItem["\0*\0poolHash"] === $this->poolHash && $castItem["\0*\0innerItem"]) {
  160. $innerItem = $castItem["\0*\0innerItem"];
  161. } elseif ($this->pool instanceof AdapterInterface) {
  162. // this is an optimization specific for AdapterInterface implementations
  163. // so we can save a round-trip to the backend by just creating a new item
  164. $innerItem = (self::$createCacheItem)($this->namespace.$castItem["\0*\0key"], null, $this->poolHash);
  165. } else {
  166. $innerItem = $this->pool->getItem($this->namespace.$castItem["\0*\0key"]);
  167. }
  168. (self::$setInnerItem)($innerItem, $item, $castItem["\0*\0expiry"]);
  169. return $this->pool->$method($innerItem);
  170. }
  171. private function generateItems(iterable $items): \Generator
  172. {
  173. $f = self::$createCacheItem;
  174. foreach ($items as $key => $item) {
  175. if ($this->namespaceLen) {
  176. $key = substr($key, $this->namespaceLen);
  177. }
  178. yield $key => $f($key, $item, $this->poolHash);
  179. }
  180. }
  181. private function getId(mixed $key): string
  182. {
  183. \assert('' !== CacheItem::validateKey($key));
  184. return $this->namespace.$key;
  185. }
  186. }