RedisCache.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 doFetchMultiple(array $keys)
  66. {
  67. return array_filter(array_combine($keys, $this->redis->mget($keys)));
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function doContains($id)
  73. {
  74. return $this->redis->exists($id);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function doSave($id, $data, $lifeTime = 0)
  80. {
  81. if ($lifeTime > 0) {
  82. return $this->redis->setex($id, $lifeTime, $data);
  83. }
  84. return $this->redis->set($id, $data);
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function doDelete($id)
  90. {
  91. return $this->redis->delete($id) > 0;
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. protected function doFlush()
  97. {
  98. return $this->redis->flushDB();
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. protected function doGetStats()
  104. {
  105. $info = $this->redis->info();
  106. return array(
  107. Cache::STATS_HITS => $info['keyspace_hits'],
  108. Cache::STATS_MISSES => $info['keyspace_misses'],
  109. Cache::STATS_UPTIME => $info['uptime_in_seconds'],
  110. Cache::STATS_MEMORY_USAGE => $info['used_memory'],
  111. Cache::STATS_MEMORY_AVAILABLE => false
  112. );
  113. }
  114. /**
  115. * Returns the serializer constant to use. If Redis is compiled with
  116. * igbinary support, that is used. Otherwise the default PHP serializer is
  117. * used.
  118. *
  119. * @return integer One of the Redis::SERIALIZER_* constants
  120. */
  121. protected function getSerializerValue()
  122. {
  123. if (defined('HHVM_VERSION')) {
  124. return Redis::SERIALIZER_PHP;
  125. }
  126. return defined('Redis::SERIALIZER_IGBINARY') ? Redis::SERIALIZER_IGBINARY : Redis::SERIALIZER_PHP;
  127. }
  128. }