Psr16Cache.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. if ($allowInt && \is_int($key)) {
  44. $item->key = (string) $key;
  45. } else {
  46. \assert('' !== CacheItem::validateKey($key));
  47. $item->key = $key;
  48. }
  49. $item->value = $value;
  50. $item->isHit = false;
  51. return $item;
  52. },
  53. null,
  54. CacheItem::class
  55. );
  56. $this->createCacheItem = function ($key, $value, $allowInt = false) use ($createCacheItem) {
  57. if (null === $this->cacheItemPrototype) {
  58. $this->get($allowInt && \is_int($key) ? (string) $key : $key);
  59. }
  60. $this->createCacheItem = $createCacheItem;
  61. return $createCacheItem($key, null, $allowInt)->set($value);
  62. };
  63. }
  64. /**
  65. * {@inheritdoc}
  66. *
  67. * @return mixed
  68. */
  69. public function get($key, $default = null)
  70. {
  71. try {
  72. $item = $this->pool->getItem($key);
  73. } catch (SimpleCacheException $e) {
  74. throw $e;
  75. } catch (Psr6CacheException $e) {
  76. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  77. }
  78. if (null === $this->cacheItemPrototype) {
  79. $this->cacheItemPrototype = clone $item;
  80. $this->cacheItemPrototype->set(null);
  81. }
  82. return $item->isHit() ? $item->get() : $default;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. *
  87. * @return bool
  88. */
  89. public function set($key, $value, $ttl = null)
  90. {
  91. try {
  92. if (null !== $f = $this->createCacheItem) {
  93. $item = $f($key, $value);
  94. } else {
  95. $item = $this->pool->getItem($key)->set($value);
  96. }
  97. } catch (SimpleCacheException $e) {
  98. throw $e;
  99. } catch (Psr6CacheException $e) {
  100. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  101. }
  102. if (null !== $ttl) {
  103. $item->expiresAfter($ttl);
  104. }
  105. return $this->pool->save($item);
  106. }
  107. /**
  108. * {@inheritdoc}
  109. *
  110. * @return bool
  111. */
  112. public function delete($key)
  113. {
  114. try {
  115. return $this->pool->deleteItem($key);
  116. } catch (SimpleCacheException $e) {
  117. throw $e;
  118. } catch (Psr6CacheException $e) {
  119. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  120. }
  121. }
  122. /**
  123. * {@inheritdoc}
  124. *
  125. * @return bool
  126. */
  127. public function clear()
  128. {
  129. return $this->pool->clear();
  130. }
  131. /**
  132. * {@inheritdoc}
  133. *
  134. * @return iterable
  135. */
  136. public function getMultiple($keys, $default = null)
  137. {
  138. if ($keys instanceof \Traversable) {
  139. $keys = iterator_to_array($keys, false);
  140. } elseif (!\is_array($keys)) {
  141. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', get_debug_type($keys)));
  142. }
  143. try {
  144. $items = $this->pool->getItems($keys);
  145. } catch (SimpleCacheException $e) {
  146. throw $e;
  147. } catch (Psr6CacheException $e) {
  148. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  149. }
  150. $values = [];
  151. if (!$this->pool instanceof AdapterInterface) {
  152. foreach ($items as $key => $item) {
  153. $values[$key] = $item->isHit() ? $item->get() : $default;
  154. }
  155. return $values;
  156. }
  157. foreach ($items as $key => $item) {
  158. if (!$item->isHit()) {
  159. $values[$key] = $default;
  160. continue;
  161. }
  162. $values[$key] = $item->get();
  163. if (!$metadata = $item->getMetadata()) {
  164. continue;
  165. }
  166. unset($metadata[CacheItem::METADATA_TAGS]);
  167. if ($metadata) {
  168. $values[$key] = ["\x9D".pack('VN', (int) (0.1 + $metadata[CacheItem::METADATA_EXPIRY] - self::METADATA_EXPIRY_OFFSET), $metadata[CacheItem::METADATA_CTIME])."\x5F" => $values[$key]];
  169. }
  170. }
  171. return $values;
  172. }
  173. /**
  174. * {@inheritdoc}
  175. *
  176. * @return bool
  177. */
  178. public function setMultiple($values, $ttl = null)
  179. {
  180. $valuesIsArray = \is_array($values);
  181. if (!$valuesIsArray && !$values instanceof \Traversable) {
  182. throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given.', get_debug_type($values)));
  183. }
  184. $items = [];
  185. try {
  186. if (null !== $f = $this->createCacheItem) {
  187. $valuesIsArray = false;
  188. foreach ($values as $key => $value) {
  189. $items[$key] = $f($key, $value, true);
  190. }
  191. } elseif ($valuesIsArray) {
  192. $items = [];
  193. foreach ($values as $key => $value) {
  194. $items[] = (string) $key;
  195. }
  196. $items = $this->pool->getItems($items);
  197. } else {
  198. foreach ($values as $key => $value) {
  199. if (\is_int($key)) {
  200. $key = (string) $key;
  201. }
  202. $items[$key] = $this->pool->getItem($key)->set($value);
  203. }
  204. }
  205. } catch (SimpleCacheException $e) {
  206. throw $e;
  207. } catch (Psr6CacheException $e) {
  208. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  209. }
  210. $ok = true;
  211. foreach ($items as $key => $item) {
  212. if ($valuesIsArray) {
  213. $item->set($values[$key]);
  214. }
  215. if (null !== $ttl) {
  216. $item->expiresAfter($ttl);
  217. }
  218. $ok = $this->pool->saveDeferred($item) && $ok;
  219. }
  220. return $this->pool->commit() && $ok;
  221. }
  222. /**
  223. * {@inheritdoc}
  224. *
  225. * @return bool
  226. */
  227. public function deleteMultiple($keys)
  228. {
  229. if ($keys instanceof \Traversable) {
  230. $keys = iterator_to_array($keys, false);
  231. } elseif (!\is_array($keys)) {
  232. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', get_debug_type($keys)));
  233. }
  234. try {
  235. return $this->pool->deleteItems($keys);
  236. } catch (SimpleCacheException $e) {
  237. throw $e;
  238. } catch (Psr6CacheException $e) {
  239. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  240. }
  241. }
  242. /**
  243. * {@inheritdoc}
  244. *
  245. * @return bool
  246. */
  247. public function has($key)
  248. {
  249. try {
  250. return $this->pool->hasItem($key);
  251. } catch (SimpleCacheException $e) {
  252. throw $e;
  253. } catch (Psr6CacheException $e) {
  254. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  255. }
  256. }
  257. }