Psr6Cache.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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\AbstractAdapter;
  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. public function __construct(CacheItemPoolInterface $pool)
  29. {
  30. $this->pool = $pool;
  31. if ($pool instanceof AbstractAdapter) {
  32. $this->createCacheItem = \Closure::bind(
  33. function ($key, $value, $allowInt = false) {
  34. if ($allowInt && \is_int($key)) {
  35. $key = (string) $key;
  36. } else {
  37. CacheItem::validateKey($key);
  38. }
  39. $f = $this->createCacheItem;
  40. return $f($key, $value, false);
  41. },
  42. $pool,
  43. AbstractAdapter::class
  44. );
  45. }
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function get($key, $default = null)
  51. {
  52. try {
  53. $item = $this->pool->getItem($key);
  54. } catch (SimpleCacheException $e) {
  55. throw $e;
  56. } catch (Psr6CacheException $e) {
  57. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  58. }
  59. return $item->isHit() ? $item->get() : $default;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function set($key, $value, $ttl = null)
  65. {
  66. try {
  67. if (null !== $f = $this->createCacheItem) {
  68. $item = $f($key, $value);
  69. } else {
  70. $item = $this->pool->getItem($key)->set($value);
  71. }
  72. } catch (SimpleCacheException $e) {
  73. throw $e;
  74. } catch (Psr6CacheException $e) {
  75. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  76. }
  77. if (null !== $ttl) {
  78. $item->expiresAfter($ttl);
  79. }
  80. return $this->pool->save($item);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function delete($key)
  86. {
  87. try {
  88. return $this->pool->deleteItem($key);
  89. } catch (SimpleCacheException $e) {
  90. throw $e;
  91. } catch (Psr6CacheException $e) {
  92. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  93. }
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function clear()
  99. {
  100. return $this->pool->clear();
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getMultiple($keys, $default = null)
  106. {
  107. if ($keys instanceof \Traversable) {
  108. $keys = iterator_to_array($keys, false);
  109. } elseif (!\is_array($keys)) {
  110. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  111. }
  112. try {
  113. $items = $this->pool->getItems($keys);
  114. } catch (SimpleCacheException $e) {
  115. throw $e;
  116. } catch (Psr6CacheException $e) {
  117. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  118. }
  119. $values = array();
  120. foreach ($items as $key => $item) {
  121. $values[$key] = $item->isHit() ? $item->get() : $default;
  122. }
  123. return $values;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function setMultiple($values, $ttl = null)
  129. {
  130. $valuesIsArray = \is_array($values);
  131. if (!$valuesIsArray && !$values instanceof \Traversable) {
  132. throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', \is_object($values) ? \get_class($values) : \gettype($values)));
  133. }
  134. $items = array();
  135. try {
  136. if (null !== $f = $this->createCacheItem) {
  137. $valuesIsArray = false;
  138. foreach ($values as $key => $value) {
  139. $items[$key] = $f($key, $value, true);
  140. }
  141. } elseif ($valuesIsArray) {
  142. $items = array();
  143. foreach ($values as $key => $value) {
  144. $items[] = (string) $key;
  145. }
  146. $items = $this->pool->getItems($items);
  147. } else {
  148. foreach ($values as $key => $value) {
  149. if (\is_int($key)) {
  150. $key = (string) $key;
  151. }
  152. $items[$key] = $this->pool->getItem($key)->set($value);
  153. }
  154. }
  155. } catch (SimpleCacheException $e) {
  156. throw $e;
  157. } catch (Psr6CacheException $e) {
  158. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  159. }
  160. $ok = true;
  161. foreach ($items as $key => $item) {
  162. if ($valuesIsArray) {
  163. $item->set($values[$key]);
  164. }
  165. if (null !== $ttl) {
  166. $item->expiresAfter($ttl);
  167. }
  168. $ok = $this->pool->saveDeferred($item) && $ok;
  169. }
  170. return $this->pool->commit() && $ok;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function deleteMultiple($keys)
  176. {
  177. if ($keys instanceof \Traversable) {
  178. $keys = iterator_to_array($keys, false);
  179. } elseif (!\is_array($keys)) {
  180. throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
  181. }
  182. try {
  183. return $this->pool->deleteItems($keys);
  184. } catch (SimpleCacheException $e) {
  185. throw $e;
  186. } catch (Psr6CacheException $e) {
  187. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  188. }
  189. }
  190. /**
  191. * {@inheritdoc}
  192. */
  193. public function has($key)
  194. {
  195. try {
  196. return $this->pool->hasItem($key);
  197. } catch (SimpleCacheException $e) {
  198. throw $e;
  199. } catch (Psr6CacheException $e) {
  200. throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  201. }
  202. }
  203. }