SimpleCacheAdapter.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface, ResettableInterface
  19. {
  20. use ProxyTrait;
  21. private $miss;
  22. public function __construct(CacheInterface $pool, string $namespace = '', int $defaultLifetime = 0)
  23. {
  24. parent::__construct($namespace, $defaultLifetime);
  25. $this->pool = $pool;
  26. $this->miss = new \stdClass();
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function doFetch(array $ids)
  32. {
  33. foreach ($this->pool->getMultiple($ids, $this->miss) as $key => $value) {
  34. if ($this->miss !== $value) {
  35. yield $key => $value;
  36. }
  37. }
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function doHave($id)
  43. {
  44. return $this->pool->has($id);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function doClear($namespace)
  50. {
  51. return $this->pool->clear();
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function doDelete(array $ids)
  57. {
  58. return $this->pool->deleteMultiple($ids);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function doSave(array $values, $lifetime)
  64. {
  65. return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime);
  66. }
  67. }