Psr16Cache.php 7.9 KB

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