RedisCache.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Cache;
  20. use Redis;
  21. /**
  22. * Redis cache provider.
  23. *
  24. * @link www.doctrine-project.org
  25. * @since 2.2
  26. * @author Osman Ungur <osmanungur@gmail.com>
  27. */
  28. class RedisCache extends CacheProvider
  29. {
  30. /**
  31. * @var Redis|null
  32. */
  33. private $redis;
  34. /**
  35. * Sets the redis instance to use.
  36. *
  37. * @param Redis $redis
  38. *
  39. * @return void
  40. */
  41. public function setRedis(Redis $redis)
  42. {
  43. $redis->setOption(Redis::OPT_SERIALIZER, $this->getSerializerValue());
  44. $this->redis = $redis;
  45. }
  46. /**
  47. * Gets the redis instance used by the cache.
  48. *
  49. * @return Redis|null
  50. */
  51. public function getRedis()
  52. {
  53. return $this->redis;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function doFetch($id)
  59. {
  60. return $this->redis->get($id);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function doContains($id)
  66. {
  67. return $this->redis->exists($id);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function doSave($id, $data, $lifeTime = 0)
  73. {
  74. if ($lifeTime > 0) {
  75. return $this->redis->setex($id, $lifeTime, $data);
  76. }
  77. return $this->redis->set($id, $data);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. protected function doDelete($id)
  83. {
  84. return $this->redis->delete($id);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function doFlush()
  90. {
  91. return $this->redis->flushDB();
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function doGetStats()
  97. {
  98. $info = $this->redis->info();
  99. return array(
  100. Cache::STATS_HITS => false,
  101. Cache::STATS_MISSES => false,
  102. Cache::STATS_UPTIME => $info['uptime_in_seconds'],
  103. Cache::STATS_MEMORY_USAGE => $info['used_memory'],
  104. Cache::STATS_MEMORY_AVAILABLE => false
  105. );
  106. }
  107. /**
  108. * Returns the serializer constant to use. If Redis is compiled with
  109. * igbinary support, that is used. Otherwise the default PHP serializer is
  110. * used.
  111. *
  112. * @return integer One of the Redis::SERIALIZER_* constants
  113. */
  114. protected function getSerializerValue()
  115. {
  116. return defined('Redis::SERIALIZER_IGBINARY') ? Redis::SERIALIZER_IGBINARY : Redis::SERIALIZER_PHP;
  117. }
  118. }