RedisCache.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use Redis;
  4. use function array_combine;
  5. use function defined;
  6. use function extension_loaded;
  7. use function is_bool;
  8. /**
  9. * Redis cache provider.
  10. *
  11. * @link www.doctrine-project.org
  12. */
  13. class RedisCache extends CacheProvider
  14. {
  15. /** @var Redis|null */
  16. private $redis;
  17. /**
  18. * Sets the redis instance to use.
  19. *
  20. * @return void
  21. */
  22. public function setRedis(Redis $redis)
  23. {
  24. $redis->setOption(Redis::OPT_SERIALIZER, $this->getSerializerValue());
  25. $this->redis = $redis;
  26. }
  27. /**
  28. * Gets the redis instance used by the cache.
  29. *
  30. * @return Redis|null
  31. */
  32. public function getRedis()
  33. {
  34. return $this->redis;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function doFetch($id)
  40. {
  41. return $this->redis->get($id);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function doFetchMultiple(array $keys)
  47. {
  48. $fetchedItems = array_combine($keys, $this->redis->mget($keys));
  49. // Redis mget returns false for keys that do not exist. So we need to filter those out unless it's the real data.
  50. $foundItems = [];
  51. foreach ($fetchedItems as $key => $value) {
  52. if ($value === false && ! $this->redis->exists($key)) {
  53. continue;
  54. }
  55. $foundItems[$key] = $value;
  56. }
  57. return $foundItems;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
  63. {
  64. if ($lifetime) {
  65. $success = true;
  66. // Keys have lifetime, use SETEX for each of them
  67. foreach ($keysAndValues as $key => $value) {
  68. if ($this->redis->setex($key, $lifetime, $value)) {
  69. continue;
  70. }
  71. $success = false;
  72. }
  73. return $success;
  74. }
  75. // No lifetime, use MSET
  76. return (bool) $this->redis->mset($keysAndValues);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. protected function doContains($id)
  82. {
  83. $exists = $this->redis->exists($id);
  84. if (is_bool($exists)) {
  85. return $exists;
  86. }
  87. return $exists > 0;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. protected function doSave($id, $data, $lifeTime = 0)
  93. {
  94. if ($lifeTime > 0) {
  95. return $this->redis->setex($id, $lifeTime, $data);
  96. }
  97. return $this->redis->set($id, $data);
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. protected function doDelete($id)
  103. {
  104. return $this->redis->delete($id) >= 0;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. protected function doDeleteMultiple(array $keys)
  110. {
  111. return $this->redis->delete($keys) >= 0;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. protected function doFlush()
  117. {
  118. return $this->redis->flushDB();
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. protected function doGetStats()
  124. {
  125. $info = $this->redis->info();
  126. return [
  127. Cache::STATS_HITS => $info['keyspace_hits'],
  128. Cache::STATS_MISSES => $info['keyspace_misses'],
  129. Cache::STATS_UPTIME => $info['uptime_in_seconds'],
  130. Cache::STATS_MEMORY_USAGE => $info['used_memory'],
  131. Cache::STATS_MEMORY_AVAILABLE => false,
  132. ];
  133. }
  134. /**
  135. * Returns the serializer constant to use. If Redis is compiled with
  136. * igbinary support, that is used. Otherwise the default PHP serializer is
  137. * used.
  138. *
  139. * @return int One of the Redis::SERIALIZER_* constants
  140. */
  141. protected function getSerializerValue()
  142. {
  143. if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) {
  144. return Redis::SERIALIZER_IGBINARY;
  145. }
  146. return Redis::SERIALIZER_PHP;
  147. }
  148. }