RedisCache.php 3.7 KB

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