forked from dcb9/yii2-phpredis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cache.php
231 lines (202 loc) · 5.7 KB
/
Cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
namespace dcb9\redis;
use Yii;
use Throwable;
use yii\di\Instance;
class Cache extends \yii\caching\Cache
{
/**
* @var Connection|string|array the Redis [[Connection]] object or the application component ID of the Redis [[Connection]].
* This can also be an array that is used to create a redis [[Connection]] instance in case you do not want do configure
* redis connection as an application component.
* After the Cache object is created, if you want to change this property, you should only assign it
* with a Redis [[Connection]] object.
*/
public $redis = 'redis';
public $silentFail = false;
/**
* Initializes the redis Cache component.
* This method will initialize the [[redis]] property to make sure it refers to a valid redis connection.
* @throws \yii\base\InvalidConfigException if [[redis]] is invalid.
*/
public function init()
{
parent::init();
$this->redis = Instance::ensure($this->redis, Connection::className());
}
/**
* @inheritdoc
*/
public function exists($key)
{
$key = $this->buildKey($key);
try {
$this->redis->open();
return (bool)$this->redis->exists($key);
} catch (Throwable $e) {
Yii::warning(
__METHOD__ . ' Open Redis connection error:' . $e->getMessage()
);
if (!$this->silentFail) {
throw $e;
}
}
}
/**
* @inheritdoc
*/
protected function getValue($key)
{
try {
$this->redis->open();
return $this->redis->get($key);
} catch (Throwable $e) {
Yii::warning(
__METHOD__ . ' Open Redis connection error:' . $e->getMessage()
);
if (!$this->silentFail) {
throw $e;
}
}
}
/**
* @inheritdoc
*/
protected function getValues($keys)
{
$result = [];
try {
$this->redis->open();
$response = $this->redis->mget($keys);
} catch (Throwable $e) {
Yii::warning(
__METHOD__ . ' Open Redis connection error:' . $e->getMessage()
);
if (!$this->silentFail) {
throw $e;
}
return $result;
}
$i = 0;
foreach ($keys as $key) {
$result[$key] = $response[$i++];
}
return $result;
}
/**
* @inheritdoc
*/
protected function setValue($key, $value, $expire)
{
try {
$this->redis->open();
if ($expire == 0) {
return (bool)$this->redis->set($key, $value);
} else {
return (bool)$this->redis->setEx($key, $expire, $value);
}
} catch (Throwable $e) {
Yii::warning(
__METHOD__ . ' Open Redis connection error:' . $e->getMessage()
);
if (!$this->silentFail) {
throw $e;
}
}
}
/**
* @inheritdoc
*/
protected function setValues($data, $expire)
{
try {
$this->redis->open();
$failedKeys = [];
if ($expire == 0) {
$this->redis->mSet($data);
} else {
$expire = (int)$expire;
$this->redis->multi();
$this->redis->mSet($data);
$index = [];
foreach ($data as $key => $value) {
$this->redis->expire($key, $expire);
$index[] = $key;
}
$result = $this->redis->exec();
array_shift($result);
foreach ($result as $i => $r) {
if ($r != 1) {
$failedKeys[] = $index[$i];
}
}
}
return $failedKeys;
} catch (Throwable $e) {
Yii::warning(
__METHOD__ . ' Open Redis connection error:' . $e->getMessage()
);
if (!$this->silentFail) {
throw $e;
}
return [];
}
}
/**
* @inheritdoc
*/
protected function addValue($key, $value, $expire)
{
try {
$this->redis->open();
if ($expire == 0) {
return (bool)$this->redis->setNx($key, $value);
}
return (bool)$this->redis->rawCommand('SET', $key, $value, 'EX', $expire, 'NX');
} catch (Throwable $e) {
Yii::warning(
__METHOD__ . ' Open Redis connection error:' . $e->getMessage()
);
if (!$this->silentFail) {
throw $e;
}
return false;
}
}
/**
* @inheritdoc
*/
protected function deleteValue($key)
{
try {
$this->redis->open();
return (bool)$this->redis->del($key);
} catch (Throwable $e) {
Yii::warning(
__METHOD__ . ' Open Redis connection error:' . $e->getMessage()
);
if (!$this->silentFail) {
throw $e;
}
return false;
}
}
/**
* @inheritdoc
*/
protected function flushValues()
{
try {
$this->redis->open();
return $this->redis->flushdb();
} catch (Throwable $e) {
Yii::warning(
__METHOD__ . ' Open Redis connection error:' . $e->getMessage()
);
if (!$this->silentFail) {
throw $e;
}
return false;
}
}
}