Psr16Adapter.php 1.8 KB

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