DoctrineProvider.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * @return mixed
  45. */
  46. protected function doFetch($id)
  47. {
  48. $item = $this->pool->getItem(rawurlencode($id));
  49. return $item->isHit() ? $item->get() : false;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. *
  54. * @return bool
  55. */
  56. protected function doContains($id)
  57. {
  58. return $this->pool->hasItem(rawurlencode($id));
  59. }
  60. /**
  61. * {@inheritdoc}
  62. *
  63. * @return bool
  64. */
  65. protected function doSave($id, $data, $lifeTime = 0)
  66. {
  67. $item = $this->pool->getItem(rawurlencode($id));
  68. if (0 < $lifeTime) {
  69. $item->expiresAfter($lifeTime);
  70. }
  71. return $this->pool->save($item->set($data));
  72. }
  73. /**
  74. * {@inheritdoc}
  75. *
  76. * @return bool
  77. */
  78. protected function doDelete($id)
  79. {
  80. return $this->pool->deleteItem(rawurlencode($id));
  81. }
  82. /**
  83. * {@inheritdoc}
  84. *
  85. * @return bool
  86. */
  87. protected function doFlush()
  88. {
  89. return $this->pool->clear();
  90. }
  91. /**
  92. * {@inheritdoc}
  93. *
  94. * @return array|null
  95. */
  96. protected function doGetStats()
  97. {
  98. return null;
  99. }
  100. }