Psr16Cache.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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;
  11. use Psr\Cache\CacheException as Psr6CacheException;
  12. use Psr\Cache\CacheItemPoolInterface;
  13. use Psr\SimpleCache\CacheException as SimpleCacheException;
  14. use Psr\SimpleCache\CacheInterface;
  15. use Symfony\Component\Cache\Adapter\AdapterInterface;
  16. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  17. use Symfony\Component\Cache\Traits\ProxyTrait;
  18. /**
  19. * Turns a PSR-6 cache into a PSR-16 one.
  20. *
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. */
  23. class Psr16Cache implements CacheInterface, PruneableInterface, ResettableInterface
  24. {
  25. use ProxyTrait;
  26. private const METADATA_EXPIRY_OFFSET = 1527506807;
  27. private ?\Closure $createCacheItem = null;
  28. private $cacheItemPrototype = null;
  29. public function __construct(CacheItemPoolInterface $pool)
  30. {
  31. $this->pool = $pool;
  32. if (!$pool instanceof AdapterInterface) {
  33. return;
  34. }
  35. $cacheItemPrototype = &$this->cacheItemPrototype;
  36. $createCacheItem = \Closure::bind(
  37. static function ($key, $value, $allowInt = false) use (&$cacheItemPrototype) {
  38. $item = clone $cacheItemPrototype;
  39. $item->poolHash = $item->innerItem = null;
  40. if ($allowInt && \is_int($key)) {
  41. $item->key = (string) $key;
  42. } else {
  43. \assert('' !== CacheItem::validateKey($key));
  44. $item->key = $key;
  45. }
  46. $item->value = $value;
  47. $item->isHit = false;
  48. return $item;
  49. },
  50. null,
  51. CacheItem::class
  52. );
  53. $this->createCacheItem = function ($key, $value, $allowInt = false) use ($createCacheItem) {
  54. if (null === $this->cacheItemPrototype) {
  55. $this->get($allowInt && \is_int($key) ? (string) $key : $key);
  56. }
  57. $this->createCacheItem = $createCacheItem;
  58. return $createCacheItem($key, null, $allowInt)->set($value);
  59. };
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function get($key, $default = null): mixed
  65. {
  66. try {
  67. $item = $this->pool->getItem($key);
  68. } catch (SimpleCacheException $e) {
  69. throw $e;
  70. } catch (Psr6CacheException $e) {
  71. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  72. }
  73. if (null === $this->cacheItemPrototype) {
  74. $this->cacheItemPrototype = clone $item;
  75. $this->cacheItemPrototype->set(null);
  76. }
  77. return $item->isHit() ? $item->get() : $default;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function set($key, $value, $ttl = null): bool
  83. {
  84. try {
  85. if (null !== $f = $this->createCacheItem) {
  86. $item = $f($key, $value);
  87. } else {
  88. $item = $this->pool->getItem($key)->set($value);
  89. }
  90. } catch (SimpleCacheException $e) {
  91. throw $e;
  92. } catch (Psr6CacheException $e) {
  93. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  94. }
  95. if (null !== $ttl) {
  96. $item->expiresAfter($ttl);
  97. }
  98. return $this->pool->save($item);
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function delete($key): bool
  104. {
  105. try {
  106. return $this->pool->deleteItem($key);
  107. } catch (SimpleCacheException $e) {
  108. throw $e;
  109. } catch (Psr6CacheException $e) {
  110. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  111. }
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function clear(): bool
  117. {
  118. return $this->pool->clear();
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getMultiple($keys, $default = null): iterable
  124. {
  125. if ($keys instanceof \Traversable) {
  126. $keys = iterator_to_array($keys, false);
  127. } elseif (!\is_array($keys)) {
  128. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', get_debug_type($keys)));
  129. }
  130. try {
  131. $items = $this->pool->getItems($keys);
  132. } catch (SimpleCacheException $e) {
  133. throw $e;
  134. } catch (Psr6CacheException $e) {
  135. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  136. }
  137. $values = [];
  138. if (!$this->pool instanceof AdapterInterface) {
  139. foreach ($items as $key => $item) {
  140. $values[$key] = $item->isHit() ? $item->get() : $default;
  141. }
  142. return $values;
  143. }
  144. foreach ($items as $key => $item) {
  145. if (!$item->isHit()) {
  146. $values[$key] = $default;
  147. continue;
  148. }
  149. $values[$key] = $item->get();
  150. if (!$metadata = $item->getMetadata()) {
  151. continue;
  152. }
  153. unset($metadata[CacheItem::METADATA_TAGS]);
  154. if ($metadata) {
  155. $values[$key] = ["\x9D".pack('VN', (int) (0.1 + $metadata[CacheItem::METADATA_EXPIRY] - self::METADATA_EXPIRY_OFFSET), $metadata[CacheItem::METADATA_CTIME])."\x5F" => $values[$key]];
  156. }
  157. }
  158. return $values;
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function setMultiple($values, $ttl = null): bool
  164. {
  165. $valuesIsArray = \is_array($values);
  166. if (!$valuesIsArray && !$values instanceof \Traversable) {
  167. throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given.', get_debug_type($values)));
  168. }
  169. $items = [];
  170. try {
  171. if (null !== $f = $this->createCacheItem) {
  172. $valuesIsArray = false;
  173. foreach ($values as $key => $value) {
  174. $items[$key] = $f($key, $value, true);
  175. }
  176. } elseif ($valuesIsArray) {
  177. $items = [];
  178. foreach ($values as $key => $value) {
  179. $items[] = (string) $key;
  180. }
  181. $items = $this->pool->getItems($items);
  182. } else {
  183. foreach ($values as $key => $value) {
  184. if (\is_int($key)) {
  185. $key = (string) $key;
  186. }
  187. $items[$key] = $this->pool->getItem($key)->set($value);
  188. }
  189. }
  190. } catch (SimpleCacheException $e) {
  191. throw $e;
  192. } catch (Psr6CacheException $e) {
  193. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  194. }
  195. $ok = true;
  196. foreach ($items as $key => $item) {
  197. if ($valuesIsArray) {
  198. $item->set($values[$key]);
  199. }
  200. if (null !== $ttl) {
  201. $item->expiresAfter($ttl);
  202. }
  203. $ok = $this->pool->saveDeferred($item) && $ok;
  204. }
  205. return $this->pool->commit() && $ok;
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function deleteMultiple($keys): bool
  211. {
  212. if ($keys instanceof \Traversable) {
  213. $keys = iterator_to_array($keys, false);
  214. } elseif (!\is_array($keys)) {
  215. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', get_debug_type($keys)));
  216. }
  217. try {
  218. return $this->pool->deleteItems($keys);
  219. } catch (SimpleCacheException $e) {
  220. throw $e;
  221. } catch (Psr6CacheException $e) {
  222. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  223. }
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. public function has($key): bool
  229. {
  230. try {
  231. return $this->pool->hasItem($key);
  232. } catch (SimpleCacheException $e) {
  233. throw $e;
  234. } catch (Psr6CacheException $e) {
  235. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  236. }
  237. }
  238. }