Psr6Cache.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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\Simple;
  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\CacheItem;
  17. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  18. use Symfony\Component\Cache\PruneableInterface;
  19. use Symfony\Component\Cache\ResettableInterface;
  20. use Symfony\Component\Cache\Traits\ProxyTrait;
  21. /**
  22. * @author Nicolas Grekas <p@tchwork.com>
  23. */
  24. class Psr6Cache implements CacheInterface, PruneableInterface, ResettableInterface
  25. {
  26. use ProxyTrait;
  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. 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, $value, $allowInt);
  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. foreach ($items as $key => $item) {
  133. $values[$key] = $item->isHit() ? $item->get() : $default;
  134. }
  135. return $values;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function setMultiple($values, $ttl = null)
  141. {
  142. $valuesIsArray = \is_array($values);
  143. if (!$valuesIsArray && !$values instanceof \Traversable) {
  144. throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', \is_object($values) ? \get_class($values) : \gettype($values)));
  145. }
  146. $items = [];
  147. try {
  148. if (null !== $f = $this->createCacheItem) {
  149. $valuesIsArray = false;
  150. foreach ($values as $key => $value) {
  151. $items[$key] = $f($key, $value, true);
  152. }
  153. } elseif ($valuesIsArray) {
  154. $items = [];
  155. foreach ($values as $key => $value) {
  156. $items[] = (string) $key;
  157. }
  158. $items = $this->pool->getItems($items);
  159. } else {
  160. foreach ($values as $key => $value) {
  161. if (\is_int($key)) {
  162. $key = (string) $key;
  163. }
  164. $items[$key] = $this->pool->getItem($key)->set($value);
  165. }
  166. }
  167. } catch (SimpleCacheException $e) {
  168. throw $e;
  169. } catch (Psr6CacheException $e) {
  170. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  171. }
  172. $ok = true;
  173. foreach ($items as $key => $item) {
  174. if ($valuesIsArray) {
  175. $item->set($values[$key]);
  176. }
  177. if (null !== $ttl) {
  178. $item->expiresAfter($ttl);
  179. }
  180. $ok = $this->pool->saveDeferred($item) && $ok;
  181. }
  182. return $this->pool->commit() && $ok;
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function deleteMultiple($keys)
  188. {
  189. if ($keys instanceof \Traversable) {
  190. $keys = iterator_to_array($keys, false);
  191. } elseif (!\is_array($keys)) {
  192. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  193. }
  194. try {
  195. return $this->pool->deleteItems($keys);
  196. } catch (SimpleCacheException $e) {
  197. throw $e;
  198. } catch (Psr6CacheException $e) {
  199. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  200. }
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function has($key)
  206. {
  207. try {
  208. return $this->pool->hasItem($key);
  209. } catch (SimpleCacheException $e) {
  210. throw $e;
  211. } catch (Psr6CacheException $e) {
  212. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  213. }
  214. }
  215. }