Psr16Cache.php 8.2 KB

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