|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Cache\Simple; |
| 13 | + |
| 14 | +use Psr\Cache\CacheItemPoolInterface; |
| 15 | +use Psr\Cache\CacheException as Psr6CacheException; |
| 16 | +use Psr\SimpleCache\CacheInterface; |
| 17 | +use Psr\SimpleCache\CacheException as SimpleCacheException; |
| 18 | +use Symfony\Component\Cache\CacheItem; |
| 19 | +use Symfony\Component\Cache\Exception\InvalidArgumentException; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Nicolas Grekas <[email protected]> |
| 23 | + */ |
| 24 | +class Psr6Cache implements CacheInterface |
| 25 | +{ |
| 26 | + private $pool; |
| 27 | + private $createCacheItem; |
| 28 | + |
| 29 | + public function __construct(CacheItemPoolInterface $pool) |
| 30 | + { |
| 31 | + $this->pool = $pool; |
| 32 | + |
| 33 | + if ($pool instanceof Adapter\AdapterInterface) { |
| 34 | + $this->createCacheItem = \Closure::bind( |
| 35 | + function ($key, $value, $allowInt = false) { |
| 36 | + if ($allowInt && is_int($key)) { |
| 37 | + $key = (string) $key; |
| 38 | + } else { |
| 39 | + CacheItem::validateKey($key); |
| 40 | + } |
| 41 | + $item = new CacheItem(); |
| 42 | + $item->key = $key; |
| 43 | + $item->value = $value; |
| 44 | + |
| 45 | + return $item; |
| 46 | + }, |
| 47 | + null, |
| 48 | + CacheItem::class |
| 49 | + ); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritdoc} |
| 55 | + */ |
| 56 | + public function get($key, $default = null) |
| 57 | + { |
| 58 | + try { |
| 59 | + $item = $this->pool->getItem($key); |
| 60 | + } catch (SimpleCacheException $e) { |
| 61 | + throw $e; |
| 62 | + } catch (Psr6CacheException $e) { |
| 63 | + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
| 64 | + } |
| 65 | + |
| 66 | + return $item->isHit() ? $item->get() : $default; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * {@inheritdoc} |
| 71 | + */ |
| 72 | + public function set($key, $value, $ttl = null) |
| 73 | + { |
| 74 | + try { |
| 75 | + if (null !== $f = $this->createCacheItem) { |
| 76 | + $item = $f($key, $value); |
| 77 | + } else { |
| 78 | + $item = $this->pool->getItem($key)->set($value); |
| 79 | + } |
| 80 | + } catch (SimpleCacheException $e) { |
| 81 | + throw $e; |
| 82 | + } catch (Psr6CacheException $e) { |
| 83 | + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
| 84 | + } |
| 85 | + if (null !== $ttl) { |
| 86 | + $item->expiresAfter($ttl); |
| 87 | + } |
| 88 | + |
| 89 | + return $this->pool->save($item); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * {@inheritdoc} |
| 94 | + */ |
| 95 | + public function delete($key) |
| 96 | + { |
| 97 | + try { |
| 98 | + return $this->pool->deleteItem($key); |
| 99 | + } catch (SimpleCacheException $e) { |
| 100 | + throw $e; |
| 101 | + } catch (Psr6CacheException $e) { |
| 102 | + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * {@inheritdoc} |
| 108 | + */ |
| 109 | + public function clear() |
| 110 | + { |
| 111 | + return $this->pool->clear(); |
| 112 | + } |
| 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 | + |
| 125 | + try { |
| 126 | + $items = $this->pool->getItems($keys); |
| 127 | + } catch (SimpleCacheException $e) { |
| 128 | + throw $e; |
| 129 | + } catch (Psr6CacheException $e) { |
| 130 | + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
| 131 | + } |
| 132 | + $values = array(); |
| 133 | + |
| 134 | + foreach ($items as $key => $item) { |
| 135 | + $values[$key] = $item->isHit() ? $item->get() : $default; |
| 136 | + } |
| 137 | + |
| 138 | + return $values; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * {@inheritdoc} |
| 143 | + */ |
| 144 | + public function setMultiple($values, $ttl = null) |
| 145 | + { |
| 146 | + $valuesIsArray = is_array($values); |
| 147 | + if (!$valuesIsArray && !$values instanceof \Traversable) { |
| 148 | + throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', is_object($values) ? get_class($values) : gettype($values))); |
| 149 | + } |
| 150 | + $items = array(); |
| 151 | + |
| 152 | + try { |
| 153 | + if (null !== $f = $this->createCacheItem) { |
| 154 | + $valuesIsArray = false; |
| 155 | + foreach ($values as $key => $value) { |
| 156 | + $items[$key] = $f($key, $value, true); |
| 157 | + } |
| 158 | + } elseif ($valuesIsArray) { |
| 159 | + $items = array(); |
| 160 | + foreach ($values as $key => $value) { |
| 161 | + $items[] = (string) $key; |
| 162 | + } |
| 163 | + $items = $this->pool->getItems($items); |
| 164 | + } else { |
| 165 | + foreach ($values as $key => $value) { |
| 166 | + if (is_int($key)) { |
| 167 | + $key = (string) $key; |
| 168 | + } |
| 169 | + $items[$key] = $this->pool->getItem($key)->set($value); |
| 170 | + } |
| 171 | + } |
| 172 | + } catch (SimpleCacheException $e) { |
| 173 | + throw $e; |
| 174 | + } catch (Psr6CacheException $e) { |
| 175 | + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
| 176 | + } |
| 177 | + $ok = true; |
| 178 | + |
| 179 | + foreach ($items as $key => $item) { |
| 180 | + if ($valuesIsArray) { |
| 181 | + $item->set($values[$key]); |
| 182 | + } |
| 183 | + if (null !== $ttl) { |
| 184 | + $item->expiresAfter($ttl); |
| 185 | + } |
| 186 | + $ok = $this->pool->saveDeferred($item) && $ok; |
| 187 | + } |
| 188 | + |
| 189 | + return $this->pool->commit() && $ok; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * {@inheritdoc} |
| 194 | + */ |
| 195 | + public function deleteMultiple($keys) |
| 196 | + { |
| 197 | + if ($keys instanceof \Traversable) { |
| 198 | + $keys = iterator_to_array($keys, false); |
| 199 | + } elseif (!is_array($keys)) { |
| 200 | + throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys))); |
| 201 | + } |
| 202 | + |
| 203 | + try { |
| 204 | + return $this->pool->deleteItems($keys); |
| 205 | + } catch (SimpleCacheException $e) { |
| 206 | + throw $e; |
| 207 | + } catch (Psr6CacheException $e) { |
| 208 | + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * {@inheritdoc} |
| 214 | + */ |
| 215 | + public function has($key) |
| 216 | + { |
| 217 | + try { |
| 218 | + return $this->pool->hasItem($key); |
| 219 | + } catch (SimpleCacheException $e) { |
| 220 | + throw $e; |
| 221 | + } catch (Psr6CacheException $e) { |
| 222 | + throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments