|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Adapter; |
| 13 | + |
| 14 | +use Psr\SimpleCache\CacheInterface; |
| 15 | +use Symfony\Component\Cache\PruneableInterface; |
| 16 | +use Symfony\Component\Cache\ResettableInterface; |
| 17 | +use Symfony\Component\Cache\Traits\ProxyTrait; |
| 18 | + |
| 19 | +/** |
| 20 | + * Turns a PSR-16 cache into a PSR-6 one. |
| 21 | + * |
| 22 | + * @author Nicolas Grekas <p@tchwork.com> |
| 23 | + */ |
| 24 | +class Psr16Adapter extends AbstractAdapter implements PruneableInterface, ResettableInterface |
| 25 | +{ |
| 26 | + use ProxyTrait; |
| 27 | + |
| 28 | + private $miss; |
| 29 | + |
| 30 | + public function __construct(CacheInterface $pool, string $namespace = '', int $defaultLifetime = 0) |
| 31 | + { |
| 32 | + parent::__construct($namespace, $defaultLifetime); |
| 33 | + |
| 34 | + $this->pool = $pool; |
| 35 | + $this->miss = new \stdClass(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritdoc} |
| 40 | + */ |
| 41 | + protected function doFetch(array $ids) |
| 42 | + { |
| 43 | + foreach ($this->pool->getMultiple($ids, $this->miss) as $key => $value) { |
| 44 | + if ($this->miss !== $value) { |
| 45 | + yield $key => $value; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + protected function doHave($id) |
| 54 | + { |
| 55 | + return $this->pool->has($id); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + protected function doClear($namespace) |
| 62 | + { |
| 63 | + return $this->pool->clear(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * {@inheritdoc} |
| 68 | + */ |
| 69 | + protected function doDelete(array $ids) |
| 70 | + { |
| 71 | + return $this->pool->deleteMultiple($ids); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * {@inheritdoc} |
| 76 | + */ |
| 77 | + protected function doSave(array $values, $lifetime) |
| 78 | + { |
| 79 | + return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime); |
| 80 | + } |
| 81 | +} |
0 commit comments