forked from dcb9/yii2-phpredis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Connection.php
342 lines (316 loc) · 10.5 KB
/
Connection.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
namespace dcb9\redis;
use Redis;
use Yii;
use yii\base\Configurable;
use RedisException;
/**
* Class Connection
* @package dcb9\redis
*/
class Connection extends Redis implements Configurable
{
/**
* @var string the hostname or ip address to use for connecting to the redis server. Defaults to 'localhost'.
* If [[unixSocket]] is specified, hostname and port will be ignored.
*/
public $hostname = 'localhost';
/**
* @var integer the port to use for connecting to the redis server. Default port is 6379.
* If [[unixSocket]] is specified, hostname and port will be ignored.
*/
public $port = 6379;
/**
* @var string the unix socket path (e.g. `/var/run/redis/redis.sock`) to use for connecting to the redis server.
* This can be used instead of [[hostname]] and [[port]] to connect to the server using a unix socket.
* If a unix socket path is specified, [[hostname]] and [[port]] will be ignored.
*/
public $unixSocket;
/**
* @var string the password for establishing DB connection. Defaults to null meaning no AUTH command is send.
* See http://redis.io/commands/auth
*/
public $password;
/**
* @var integer the redis database to use. This is an integer value starting from 0. Defaults to 0.
*/
public $database = 0;
/**
* @var float value in seconds (optional, default is 0.0 meaning unlimited)
*/
public $connectionTimeout = 0.0;
/**
* @var float value in seconds (optional, default is 0.0 meaning unlimited)
*/
public $readTimeout = 0.0;
/**
* Retry interval - value in ms
*
* @var integer
*/
public $retryInterval = 0;
/**
* Number of retries.
*
* @var integer
*/
public $retries = 0;
/**
* Constructor.
* The default implementation does two things:
*
* - Initializes the object with the given configuration `$config`.
* - Call [[init()]].
*
* If this method is overridden in a child class, it is recommended that
*
* - the last parameter of the constructor is a configuration array, like `$config` here.
* - call the parent implementation at the end of the constructor.
*
* @param array $config name-value pairs that will be used to initialize the object properties
*/
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
}
private $multiExecCommand = false;
private $multiCommands = [];
private $redisRetryCommands = [
'retrySet' => 'SET',
'retryGet' => 'GET',
'retryKeys' => 'KEYS',
'retryDel' => 'DEL',
'retryTTL' => 'TTL',
'retryMulti' => 'MULTI',
'retrySAdd' => 'SADD',
'retryExec' => 'EXEC',
'retryExpire' => 'EXPIRE',
'retrySMembers' => 'sMembers',
'retryUnlink' => 'UNLINK',
'retryPublish' => 'PUBLISH',
];
/**
* Allows issuing all supported commands via magic methods.
*
* @param string $command - name of the missing method to execute
* @param array $params - method call arguments
*
* @return mixed
*/
public function __call($command, $params)
{
if (in_array($command, array_keys($this->redisRetryCommands))) {
if ($command === 'retryMulti') {
$this->multiExecCommand = true;
$this->multiCommands = [];
$this->multiCommands[] = $this->computeRawCommand($command, $params);
return $this;
}
if ($this->multiExecCommand === true && $command !== 'retryExec') {
// Chain all commands between retryMulti and retryExec.
$this->multiCommands[] = $this->computeRawCommand($command, $params);
return $this;
}
if ($this->multiExecCommand === true && $command === 'retryExec') {
$this->multiCommands[] = $this->computeRawCommand($command, $params);
$this->multiExecCommand = false;
$responseMultiCommand = $this->executeMultiCommand($this->multiCommands);
$this->multiCommands = [];
return $responseMultiCommand;
}
if ($this->multiExecCommand === false) {
return $this->executeCommand($this->computeRawCommand($command, $params));
}
}
return parent::__call($command, $params);
}
private function computeRawCommand($command, $params)
{
$completeCommand = [];
$completeCommand[] = $this->redisRetryCommands[$command];
foreach ($params as $param) {
$completeCommand[] = $param;
}
return $completeCommand;
}
/**
* Returns the fully qualified name of this class.
* @return string the fully qualified name of this class.
*/
public static function className()
{
return get_called_class();
}
/**
* Establishes a DB connection.
* It does nothing if a DB connection has already been established.
* @throws RedisException if connection fails
* @example 问题详细描述 https://bugs.php.net/bug.php?id=46851 php_redis.so 版本为4.0.2时会出现一条警告
* @see connect()
* @param string $host
* @param int $port
* @param float $timeout
* @param int $retry_interval
* @return bool
*/
public function open( $host = null, $port = null, $timeout = null, $retry_interval = 0 )
{
if ($this->unixSocket !== null) {
$isConnected = $this->connect($this->unixSocket);
} else {
if(is_null($host)){
$host = $this->hostname;
}
if(is_null($port)){
$port = $this->port;
}
if(is_null($timeout)){
$timeout = $this->connectionTimeout;
}
$isConnected = $this->connect($host, $port, $timeout, null, $retry_interval, $this->readTimeout);
}
if ($isConnected === false) {
throw new RedisException('Connect to redis server error.');
}
if ($this->password !== null) {
$this->auth($this->password);
}
if ($this->database !== null) {
$this->select($this->database);
}
}
/**
* @return bool
*/
public function ping()
{
return parent::ping() === '+PONG';
}
public function flushDB($async = null)
{
return parent::flushDB($async);
}
/**
* Execute Multi exec command.
*
* @param array $commands - Command to run on Redis.
*
* @return mixed
*/
public function executeMultiCommand(array $commands = [])
{
// Run Redis command with retries.
if ($this->retries > 0) {
return $this->retryExecuteMultiCommand($commands);
}
return $this->executeMultiCommandOnce($commands);
}
/**
* Execute Redis command once. Will throw an Redis Exception in case there is a connection error.
*
* @param array $commands - Command to run on Redis.
*
* @return mixed
*/
protected function executeMultiCommandOnce(array $commands = [])
{
$this->open();
$responses = [];
foreach ($commands as $i => $command) {
$responses[$i] = $this->sendRawCommand($command);
}
return $responses;
}
/**
* Execute Redis MULTI command with retry and retry interval.
* No Exception is thrown in case there is a connection error.
*
* @param array $commands - Command (multi dimensional array of commands).
*
* @return mixed
*/
protected function retryExecuteMultiCommand(array $commands = [])
{
$tries = $this->retries;
while ($tries-- > 0) {
try {
// Try to open a Redis connection and execute the command.
return $this->executeMultiCommandOnce($commands);
} catch (RedisException $exception) {
// Log any exception with Yii error.
Yii::error($exception, __METHOD__);
// In case Redis is not accessible, close the connection, wait for the retry interval.
$this->close();
if ($this->retryInterval > 0) {
usleep($this->retryInterval * 1000);
}
}
}
}
/**
* Execute Redis command.
*
* @param array $command - Command to run on Redis.
*
* @return mixed
*/
public function executeCommand(array $command = [])
{
// Run Redis command with retries.
if ($this->retries > 0) {
return $this->retryExecuteCommand($command);
}
return $this->executeCommandOnce($command);
}
/**
* Execute Redis command once. Will throw an Redis Exception in case there is a connection error.
*
* @param array $command - Command to run on Redis.
*
* @return mixed
*/
protected function executeCommandOnce(array $command = [])
{
$this->open();
return $this->sendRawCommand($command);
}
/**
* Execute Redis command with retry and retry interval. No Exception is thrown in case there is a connection error.
*
* @param array $command - Command.
*
* @return mixed
*/
protected function retryExecuteCommand(array $command = [])
{
$tries = $this->retries;
while ($tries-- > 0) {
try {
// Try to open a Redis connection and execute the command.
return $this->executeCommandOnce($command);
} catch (RedisException $exception) {
// Log any exception with Yii error.
Yii::error($exception, __METHOD__);
// In case Redis is not accessible, close the connection, wait for the retry interval.
$this->close();
if ($this->retryInterval > 0) {
usleep($this->retryInterval * 1000);
}
}
}
}
/**
* Run Redis rawCommand with function array.
*
* @param array $command - Command
*
* @return void
*/
protected function sendRawCommand(array $command)
{
$response = call_user_func_array(array($this, 'rawCommand'), $command);
return $response;
}
}