Cache.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\redis;
  8. use Yii;
  9. use yii\di\Instance;
  10. /**
  11. * Redis Cache implements a cache application component based on [redis](http://redis.io/) key-value store.
  12. *
  13. * Redis Cache requires redis version 2.6.12 or higher to work properly.
  14. *
  15. * It needs to be configured with a redis [[Connection]] that is also configured as an application component.
  16. * By default it will use the `redis` application component.
  17. *
  18. * See [[Cache]] manual for common cache operations that redis Cache supports.
  19. *
  20. * Unlike the [[Cache]], redis Cache allows the expire parameter of [[set]], [[add]], [[mset]] and [[madd]] to
  21. * be a floating point number, so you may specify the time in milliseconds (e.g. 0.1 will be 100 milliseconds).
  22. *
  23. * To use redis Cache as the cache application component, configure the application as follows,
  24. *
  25. * ~~~
  26. * [
  27. * 'components' => [
  28. * 'cache' => [
  29. * 'class' => 'yii\redis\Cache',
  30. * 'redis' => [
  31. * 'hostname' => 'localhost',
  32. * 'port' => 6379,
  33. * 'database' => 0,
  34. * ]
  35. * ],
  36. * ],
  37. * ]
  38. * ~~~
  39. *
  40. * Or if you have configured the redis [[Connection]] as an application component, the following is sufficient:
  41. *
  42. * ~~~
  43. * [
  44. * 'components' => [
  45. * 'cache' => [
  46. * 'class' => 'yii\redis\Cache',
  47. * // 'redis' => 'redis' // id of the connection application component
  48. * ],
  49. * ],
  50. * ]
  51. * ~~~
  52. *
  53. * @author Carsten Brandt <mail@cebe.cc>
  54. * @since 2.0
  55. */
  56. class Cache extends \yii\caching\Cache
  57. {
  58. /**
  59. * @var Connection|string|array the Redis [[Connection]] object or the application component ID of the Redis [[Connection]].
  60. * This can also be an array that is used to create a redis [[Connection]] instance in case you do not want do configure
  61. * redis connection as an application component.
  62. * After the Cache object is created, if you want to change this property, you should only assign it
  63. * with a Redis [[Connection]] object.
  64. */
  65. public $redis = 'redis';
  66. /**
  67. * Initializes the redis Cache component.
  68. * This method will initialize the [[redis]] property to make sure it refers to a valid redis connection.
  69. * @throws \yii\base\InvalidConfigException if [[redis]] is invalid.
  70. */
  71. public function init()
  72. {
  73. parent::init();
  74. $this->redis = Instance::ensure($this->redis, Connection::className());
  75. }
  76. /**
  77. * Checks whether a specified key exists in the cache.
  78. * This can be faster than getting the value from the cache if the data is big.
  79. * Note that this method does not check whether the dependency associated
  80. * with the cached data, if there is any, has changed. So a call to [[get]]
  81. * may return false while exists returns true.
  82. * @param mixed $key a key identifying the cached value. This can be a simple string or
  83. * a complex data structure consisting of factors representing the key.
  84. * @return bool true if a value exists in cache, false if the value is not in the cache or expired.
  85. */
  86. public function exists($key)
  87. {
  88. return (bool) $this->redis->executeCommand('EXISTS', [$this->buildKey($key)]);
  89. }
  90. /**
  91. * @inheritdoc
  92. */
  93. protected function getValue($key)
  94. {
  95. return $this->redis->executeCommand('GET', [$key]);
  96. }
  97. /**
  98. * @inheritdoc
  99. */
  100. protected function getValues($keys)
  101. {
  102. $response = $this->redis->executeCommand('MGET', $keys);
  103. $result = [];
  104. $i = 0;
  105. foreach ($keys as $key) {
  106. $result[$key] = $response[$i++];
  107. }
  108. return $result;
  109. }
  110. /**
  111. * @inheritdoc
  112. */
  113. protected function setValue($key, $value, $expire)
  114. {
  115. if ($expire == 0) {
  116. return (bool) $this->redis->executeCommand('SET', [$key, $value]);
  117. } else {
  118. $expire = (int) ($expire * 1000);
  119. return (bool) $this->redis->executeCommand('SET', [$key, $value, 'PX', $expire]);
  120. }
  121. }
  122. /**
  123. * @inheritdoc
  124. */
  125. protected function setValues($data, $expire)
  126. {
  127. $args = [];
  128. foreach ($data as $key => $value) {
  129. $args[] = $key;
  130. $args[] = $value;
  131. }
  132. $failedKeys = [];
  133. if ($expire == 0) {
  134. $this->redis->executeCommand('MSET', $args);
  135. } else {
  136. $expire = (int) ($expire * 1000);
  137. $this->redis->executeCommand('MULTI');
  138. $this->redis->executeCommand('MSET', $args);
  139. $index = [];
  140. foreach ($data as $key => $value) {
  141. $this->redis->executeCommand('PEXPIRE', [$key, $expire]);
  142. $index[] = $key;
  143. }
  144. $result = $this->redis->executeCommand('EXEC');
  145. array_shift($result);
  146. foreach ($result as $i => $r) {
  147. if ($r != 1) {
  148. $failedKeys[] = $index[$i];
  149. }
  150. }
  151. }
  152. return $failedKeys;
  153. }
  154. /**
  155. * @inheritdoc
  156. */
  157. protected function addValue($key, $value, $expire)
  158. {
  159. if ($expire == 0) {
  160. return (bool) $this->redis->executeCommand('SET', [$key, $value, 'NX']);
  161. } else {
  162. $expire = (int) ($expire * 1000);
  163. return (bool) $this->redis->executeCommand('SET', [$key, $value, 'PX', $expire, 'NX']);
  164. }
  165. }
  166. /**
  167. * @inheritdoc
  168. */
  169. protected function deleteValue($key)
  170. {
  171. return (bool) $this->redis->executeCommand('DEL', [$key]);
  172. }
  173. /**
  174. * @inheritdoc
  175. */
  176. protected function flushValues()
  177. {
  178. return $this->redis->executeCommand('FLUSHDB');
  179. }
  180. }