Psr16Cache.php 8.5 KB

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