Psr16Cache.php 8.2 KB

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