SimpleCacheAdapter.php 1.7 KB

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