Psr16Cache.php 8.2 KB

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