Psr16Cache.php 8.1 KB

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