Cache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace dcb9\redis;
  3. use yii\di\Instance;
  4. class Cache extends \yii\caching\Cache
  5. {
  6. /**
  7. * @var Connection|string|array the Redis [[Connection]] object or the application component ID of the Redis [[Connection]].
  8. * This can also be an array that is used to create a redis [[Connection]] instance in case you do not want do configure
  9. * redis connection as an application component.
  10. * After the Cache object is created, if you want to change this property, you should only assign it
  11. * with a Redis [[Connection]] object.
  12. */
  13. public $redis = 'redis';
  14. /**
  15. * Initializes the redis Cache component.
  16. * This method will initialize the [[redis]] property to make sure it refers to a valid redis connection.
  17. * @throws \yii\base\InvalidConfigException if [[redis]] is invalid.
  18. */
  19. public function init()
  20. {
  21. parent::init();
  22. $this->redis = Instance::ensure($this->redis, Connection::className());
  23. $this->redis->open();
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. public function exists($key)
  29. {
  30. $key = $this->buildKey($key);
  31. return (bool)$this->redis->exists($key);
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. protected function getValue($key)
  37. {
  38. return $this->redis->get($key);
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. protected function getValues($keys)
  44. {
  45. $response = $this->redis->mget($keys);
  46. $result = [];
  47. $i = 0;
  48. foreach ($keys as $key) {
  49. $result[$key] = $response[$i++];
  50. }
  51. return $result;
  52. }
  53. /**
  54. * @inheritdoc
  55. */
  56. protected function setValue($key, $value, $expire)
  57. {
  58. if ($expire == 0) {
  59. return (bool)$this->redis->set($key, $value);
  60. } else {
  61. return (bool)$this->redis->setEx($key, $expire, $value);
  62. }
  63. }
  64. /**
  65. * @inheritdoc
  66. */
  67. protected function setValues($data, $expire)
  68. {
  69. $failedKeys = [];
  70. if ($expire == 0) {
  71. $this->redis->mSet($data);
  72. } else {
  73. $expire = (int)$expire;
  74. $this->redis->multi();
  75. $this->redis->mSet($data);
  76. $index = [];
  77. foreach ($data as $key => $value) {
  78. $this->redis->expire($key, $expire);
  79. $index[] = $key;
  80. }
  81. $result = $this->redis->exec();
  82. array_shift($result);
  83. foreach ($result as $i => $r) {
  84. if ($r != 1) {
  85. $failedKeys[] = $index[$i];
  86. }
  87. }
  88. }
  89. return $failedKeys;
  90. }
  91. /**
  92. * @inheritdoc
  93. */
  94. protected function addValue($key, $value, $expire)
  95. {
  96. if ($expire == 0) {
  97. return (bool)$this->redis->setNx($key, $value);
  98. }
  99. return (bool)$this->redis->rawCommand('SET', $key, $value, 'EX', $expire, 'NX');
  100. }
  101. /**
  102. * @inheritdoc
  103. */
  104. protected function deleteValue($key)
  105. {
  106. return (bool)$this->redis->del($key);
  107. }
  108. /**
  109. * @inheritdoc
  110. */
  111. protected function flushValues()
  112. {
  113. return $this->redis->flushdb();
  114. }
  115. }