RedisCache.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 = array_combine($keys, $this->redis->mget($keys));
  68. // Redis mget returns false for keys that do not exist. So we need to filter those out unless it's the real data.
  69. $foundItems = array();
  70. foreach ($fetchedItems as $key => $value) {
  71. if (false !== $value || $this->redis->exists($key)) {
  72. $foundItems[$key] = $value;
  73. }
  74. }
  75. return $foundItems;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
  81. {
  82. if ($lifetime) {
  83. $success = true;
  84. // Keys have lifetime, use SETEX for each of them
  85. foreach ($keysAndValues as $key => $value) {
  86. if (!$this->redis->setex($key, $lifetime, $value)) {
  87. $success = false;
  88. }
  89. }
  90. return $success;
  91. }
  92. // No lifetime, use MSET
  93. return (bool) $this->redis->mset($keysAndValues);
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. protected function doContains($id)
  99. {
  100. return $this->redis->exists($id);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. protected function doSave($id, $data, $lifeTime = 0)
  106. {
  107. if ($lifeTime > 0) {
  108. return $this->redis->setex($id, $lifeTime, $data);
  109. }
  110. return $this->redis->set($id, $data);
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. protected function doDelete($id)
  116. {
  117. return $this->redis->delete($id) >= 0;
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. protected function doFlush()
  123. {
  124. return $this->redis->flushDB();
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. protected function doGetStats()
  130. {
  131. $info = $this->redis->info();
  132. return array(
  133. Cache::STATS_HITS => $info['keyspace_hits'],
  134. Cache::STATS_MISSES => $info['keyspace_misses'],
  135. Cache::STATS_UPTIME => $info['uptime_in_seconds'],
  136. Cache::STATS_MEMORY_USAGE => $info['used_memory'],
  137. Cache::STATS_MEMORY_AVAILABLE => false
  138. );
  139. }
  140. /**
  141. * Returns the serializer constant to use. If Redis is compiled with
  142. * igbinary support, that is used. Otherwise the default PHP serializer is
  143. * used.
  144. *
  145. * @return integer One of the Redis::SERIALIZER_* constants
  146. */
  147. protected function getSerializerValue()
  148. {
  149. if (defined('HHVM_VERSION')) {
  150. return Redis::SERIALIZER_PHP;
  151. }
  152. if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) {
  153. return Redis::SERIALIZER_IGBINARY;
  154. }
  155. return Redis::SERIALIZER_PHP;
  156. }
  157. }