DoctrineProvider.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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;
  11. use Doctrine\Common\Cache\CacheProvider;
  12. use Psr\Cache\CacheItemPoolInterface;
  13. use Symfony\Contracts\Service\ResetInterface;
  14. /**
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class DoctrineProvider extends CacheProvider implements PruneableInterface, ResettableInterface
  18. {
  19. private $pool;
  20. public function __construct(CacheItemPoolInterface $pool)
  21. {
  22. $this->pool = $pool;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function prune()
  28. {
  29. return $this->pool instanceof PruneableInterface && $this->pool->prune();
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function reset()
  35. {
  36. if ($this->pool instanceof ResetInterface) {
  37. $this->pool->reset();
  38. }
  39. $this->setNamespace($this->getNamespace());
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function doFetch($id)
  45. {
  46. $item = $this->pool->getItem(rawurlencode($id));
  47. return $item->isHit() ? $item->get() : false;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function doContains($id)
  53. {
  54. return $this->pool->hasItem(rawurlencode($id));
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function doSave($id, $data, $lifeTime = 0)
  60. {
  61. $item = $this->pool->getItem(rawurlencode($id));
  62. if (0 < $lifeTime) {
  63. $item->expiresAfter($lifeTime);
  64. }
  65. return $this->pool->save($item->set($data));
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function doDelete($id)
  71. {
  72. return $this->pool->deleteItem(rawurlencode($id));
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function doFlush()
  78. {
  79. return $this->pool->clear();
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. protected function doGetStats()
  85. {
  86. return null;
  87. }
  88. }