RedisCache.php 4.4 KB

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