72 parent::__construct( $params );
73 $redisConf = [
'serializer' =>
'none' ];
74 foreach ( [
'connectTimeout',
'persistent',
'password' ] as $opt ) {
75 if ( isset( $params[$opt] ) ) {
76 $redisConf[$opt] = $params[$opt];
81 $this->servers = $params[
'servers'];
82 foreach ( $this->servers as $key => $server ) {
83 $this->serverTagMap[is_int( $key ) ? $server : $key] = $server;
86 $this->automaticFailover = $params[
'automaticFailover'] ??
true;
91 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
101 $value = $conn->get( $key );
104 }
catch ( RedisException $e ) {
109 $this->
logRequest(
'get', $key, $conn->getServer(), $e );
114 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
125 $result = $conn->setex( $key, $ttl, $this->
serialize( $value ) );
127 $result = $conn->set( $key, $this->
serialize( $value ) );
129 }
catch ( RedisException $e ) {
134 $this->
logRequest(
'set', $key, $conn->getServer(), $e );
148 $result = ( $conn->del( $key ) !== false );
149 }
catch ( RedisException $e ) {
154 $this->
logRequest(
'delete', $key, $conn->getServer(), $e );
163 foreach (
$keys as $key ) {
166 $server = $conn->getServer();
167 $conns[$server] = $conn;
168 $batches[$server][] = $key;
173 foreach ( $batches as $server => $batchKeys ) {
174 $conn = $conns[$server];
179 $conn->multi( Redis::PIPELINE );
180 foreach ( $batchKeys as $key ) {
183 $batchResult = $conn->exec();
184 if ( $batchResult ===
false ) {
185 $this->
logRequest(
'get', implode(
',', $batchKeys ), $server,
true );
189 foreach ( $batchResult as $i => $value ) {
190 if ( $value !==
false ) {
191 $result[$batchKeys[$i]] = $this->
unserialize( $value );
194 }
catch ( RedisException $e ) {
198 $this->
logRequest(
'get', implode(
',', $batchKeys ), $server, $e );
204 protected function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
208 foreach ( $data as $key => $value ) {
211 $server = $conn->getServer();
212 $conns[$server] = $conn;
213 $batches[$server][] = $key;
218 $op = $ttl ?
'setex' :
'set';
221 foreach ( $batches as $server => $batchKeys ) {
222 $conn = $conns[$server];
227 $conn->multi( Redis::PIPELINE );
228 foreach ( $batchKeys as $key ) {
230 $conn->setex( $key, $ttl, $this->
serialize( $data[$key] ) );
232 $conn->set( $key, $this->
serialize( $data[$key] ) );
235 $batchResult = $conn->exec();
236 if ( $batchResult ===
false ) {
237 $this->
logRequest( $op, implode(
',', $batchKeys ), $server,
true );
240 $result = $result && !in_array(
false, $batchResult,
true );
241 }
catch ( RedisException $e ) {
246 $this->
logRequest( $op, implode(
',', $batchKeys ), $server, $e );
256 foreach (
$keys as $key ) {
259 $server = $conn->getServer();
260 $conns[$server] = $conn;
261 $batches[$server][] = $key;
266 foreach ( $batches as $server => $batchKeys ) {
267 $conn = $conns[$server];
272 $conn->multi( Redis::PIPELINE );
273 foreach ( $batchKeys as $key ) {
276 $batchResult = $conn->exec();
277 if ( $batchResult ===
false ) {
278 $this->
logRequest(
'delete', implode(
',', $batchKeys ), $server,
true );
282 $result = $result && !in_array(
false, $batchResult,
true );
283 }
catch ( RedisException $e ) {
288 $this->
logRequest(
'delete', implode(
',', $batchKeys ), $server, $e );
298 foreach (
$keys as $key ) {
301 $server = $conn->getServer();
302 $conns[$server] = $conn;
303 $batches[$server][] = $key;
310 : ( $relative ?
'expire' :
'expireAt' );
313 foreach ( $batches as $server => $batchKeys ) {
314 $conn = $conns[$server];
318 $conn->multi( Redis::PIPELINE );
319 foreach ( $batchKeys as $key ) {
320 if ( $exptime == self::TTL_INDEFINITE ) {
321 $conn->persist( $key );
322 } elseif ( $relative ) {
328 $batchResult = $conn->exec();
329 if ( $batchResult ===
false ) {
330 $this->
logRequest( $op, implode(
',', $batchKeys ), $server,
true );
333 $result = in_array(
false, $batchResult,
true ) ? false : $result;
334 }
catch ( RedisException $e ) {
339 $this->
logRequest( $op, implode(
',', $batchKeys ), $server, $e );
345 protected function doAdd( $key, $value, $expiry = 0, $flags = 0 ) {
353 $result = $conn->set(
356 $ttl ? [
'nx',
'ex' => $ttl ] : [
'nx' ]
358 }
catch ( RedisException $e ) {
363 $this->
logRequest(
'add', $key, $conn->getServer(), $result );
368 public function incr( $key, $value = 1, $flags = 0 ) {
375 if ( !$conn->exists( $key ) ) {
379 $result = $conn->incrBy( $key, $value );
380 }
catch ( RedisException $e ) {
385 $this->
logRequest(
'incr', $key, $conn->getServer(), $result );
390 public function decr( $key, $value = 1, $flags = 0 ) {
397 if ( !$conn->exists( $key ) ) {
401 $result = $conn->decrBy( $key, $value );
402 }
catch ( RedisException $e ) {
407 $this->
logRequest(
'decr', $key, $conn->getServer(), $result );
420 if ( $exptime == self::TTL_INDEFINITE ) {
421 $result = $conn->persist( $key );
422 $this->
logRequest(
'persist', $key, $conn->getServer(), $result );
423 } elseif ( $relative ) {
425 $this->
logRequest(
'expire', $key, $conn->getServer(), $result );
428 $this->
logRequest(
'expireAt', $key, $conn->getServer(), $result );
430 }
catch ( RedisException $e ) {
443 $candidates = array_keys( $this->serverTagMap );
445 if ( count( $this->servers ) > 1 ) {
447 if ( !$this->automaticFailover ) {
448 $candidates = array_slice( $candidates, 0, 1 );
452 while ( ( $tag = array_shift( $candidates ) ) !==
null ) {
453 $server = $this->serverTagMap[$tag];
454 $conn = $this->redisPool->getConnection( $server, $this->logger );
464 if ( $this->automaticFailover && $candidates ) {
467 $info = $conn->info();
468 if ( ( $info[
'master_link_status'] ??
null ) ===
'down' ) {
475 }
catch ( RedisException $e ) {
477 $this->redisPool->handleError( $conn, $e );
495 $this->logger->error(
"Redis error: $msg" );
508 $this->redisPool->handleError( $conn, $e );
519 $this->
debug(
"$op($keys) on $server: " . ( $e ?
"failure" :
"success" ) );