|
13 | 13 |
|
14 | 14 | use Psr\Cache\CacheItemInterface;
|
15 | 15 | use Psr\Log\LoggerAwareInterface;
|
| 16 | +use Psr\Log\LoggerAwareTrait; |
16 | 17 | use Symfony\Component\Cache\CacheItem;
|
17 | 18 | use Symfony\Component\Cache\ResettableInterface;
|
18 | 19 | use Symfony\Component\Cache\Traits\ArrayTrait;
|
|
23 | 24 | */
|
24 | 25 | class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
|
25 | 26 | {
|
26 |
| - use ArrayTrait; |
| 27 | + use LoggerAwareTrait; |
27 | 28 |
|
| 29 | + private $storeSerialized; |
28 | 30 | private $createCacheItem;
|
| 31 | + private $values = array(); |
| 32 | + private $expiries = array(); |
29 | 33 |
|
30 | 34 | /**
|
31 | 35 | * @param int $defaultLifetime
|
@@ -64,6 +68,19 @@ public function get(string $key, callable $callback, float $beta = null)
|
64 | 68 | return $item->get();
|
65 | 69 | }
|
66 | 70 |
|
| 71 | + /** |
| 72 | + * {@inheritdoc} |
| 73 | + */ |
| 74 | + public function hasItem($key) |
| 75 | + { |
| 76 | + if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > microtime(true)) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + CacheItem::validateKey($key); |
| 80 | + |
| 81 | + return isset($this->expiries[$key]) && !$this->deleteItem($key); |
| 82 | + } |
| 83 | + |
67 | 84 | /**
|
68 | 85 | * {@inheritdoc}
|
69 | 86 | */
|
@@ -159,4 +176,126 @@ public function delete(string $key): bool
|
159 | 176 | {
|
160 | 177 | return $this->deleteItem($key);
|
161 | 178 | }
|
| 179 | + |
| 180 | + /** |
| 181 | + * {@inheritdoc} |
| 182 | + */ |
| 183 | + public function clear() |
| 184 | + { |
| 185 | + $this->values = $this->expiries = array(); |
| 186 | + |
| 187 | + return true; |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * {@inheritdoc} |
| 192 | + */ |
| 193 | + public function deleteItem($key) |
| 194 | + { |
| 195 | + if (!\is_string($key) || !isset($this->expiries[$key])) { |
| 196 | + CacheItem::validateKey($key); |
| 197 | + } |
| 198 | + unset($this->values[$key], $this->expiries[$key]); |
| 199 | + |
| 200 | + return true; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * {@inheritdoc} |
| 205 | + */ |
| 206 | + public function reset() |
| 207 | + { |
| 208 | + $this->clear(); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Returns all cached values, with cache miss as null. |
| 213 | + * |
| 214 | + * @return array |
| 215 | + */ |
| 216 | + public function getValues() |
| 217 | + { |
| 218 | + if (!$this->storeSerialized) { |
| 219 | + return $this->values; |
| 220 | + } |
| 221 | + |
| 222 | + $values = $this->values; |
| 223 | + foreach ($values as $k => $v) { |
| 224 | + if (null === $v || 'N;' === $v) { |
| 225 | + continue; |
| 226 | + } |
| 227 | + if (!\is_string($v) || !isset($v[2]) || ':' !== $v[1]) { |
| 228 | + $values[$k] = serialize($v); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + return $values; |
| 233 | + } |
| 234 | + |
| 235 | + private function generateItems(array $keys, $now, $f) |
| 236 | + { |
| 237 | + foreach ($keys as $i => $key) { |
| 238 | + if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] > $now || !$this->deleteItem($key))) { |
| 239 | + $this->values[$key] = $value = null; |
| 240 | + } else { |
| 241 | + $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key]; |
| 242 | + } |
| 243 | + unset($keys[$i]); |
| 244 | + |
| 245 | + yield $key => $f($key, $value, $isHit); |
| 246 | + } |
| 247 | + |
| 248 | + foreach ($keys as $key) { |
| 249 | + yield $key => $f($key, null, false); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + private function freeze($value) |
| 254 | + { |
| 255 | + if (null === $value) { |
| 256 | + return 'N;'; |
| 257 | + } |
| 258 | + if (\is_string($value)) { |
| 259 | + // Serialize strings if they could be confused with serialized objects or arrays |
| 260 | + if ('N;' === $value || (isset($value[2]) && ':' === $value[1])) { |
| 261 | + return serialize($value); |
| 262 | + } |
| 263 | + } elseif (!\is_scalar($value)) { |
| 264 | + try { |
| 265 | + $serialized = serialize($value); |
| 266 | + } catch (\Exception $e) { |
| 267 | + $type = \is_object($value) ? \get_class($value) : \gettype($value); |
| 268 | + CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e)); |
| 269 | + |
| 270 | + return; |
| 271 | + } |
| 272 | + // Keep value serialized if it contains any objects or any internal references |
| 273 | + if ('C' === $serialized[0] || 'O' === $serialized[0] || preg_match('/;[OCRr]:[1-9]/', $serialized)) { |
| 274 | + return $serialized; |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + return $value; |
| 279 | + } |
| 280 | + |
| 281 | + private function unfreeze(string $key, bool &$isHit) |
| 282 | + { |
| 283 | + if ('N;' === $value = $this->values[$key]) { |
| 284 | + return null; |
| 285 | + } |
| 286 | + if (\is_string($value) && isset($value[2]) && ':' === $value[1]) { |
| 287 | + try { |
| 288 | + $value = unserialize($value); |
| 289 | + } catch (\Exception $e) { |
| 290 | + CacheItem::log($this->logger, 'Failed to unserialize key "{key}"', array('key' => $key, 'exception' => $e)); |
| 291 | + $value = false; |
| 292 | + } |
| 293 | + if (false === $value) { |
| 294 | + $this->values[$key] = $value = null; |
| 295 | + $isHit = false; |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + return $value; |
| 300 | + } |
162 | 301 | }
|
0 commit comments