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;
92 protected function doGet( $key, $flags = 0, &$casToken =
null ) {
93 $getToken = ( $casToken === self::PASS_BY_REF );
103 $blob = $conn->get( $key );
104 if ( $getToken &&
$blob !==
false ) {
108 $valueSize = strlen(
$blob );
109 }
catch ( RedisException $e ) {
115 $this->
logRequest(
'get', $key, $conn->getServer(), $e );
117 $this->
updateOpStats( self::METRIC_OP_GET, [ $key => [
null, $valueSize ] ] );
122 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
139 }
catch ( RedisException $e ) {
144 $this->
logRequest(
'set', $key, $conn->getServer(), $e );
146 $this->
updateOpStats( self::METRIC_OP_SET, [ $key => [ $valueSize,
null ] ] );
160 $result = ( $conn->del( $key ) !== false );
161 }
catch ( RedisException $e ) {
166 $this->
logRequest(
'delete', $key, $conn->getServer(), $e );
177 foreach (
$keys as $key ) {
180 $server = $conn->getServer();
181 $conns[$server] = $conn;
182 $batches[$server][] = $key;
187 foreach ( $batches as $server => $batchKeys ) {
188 $conn = $conns[$server];
193 $conn->multi( Redis::PIPELINE );
194 foreach ( $batchKeys as $key ) {
197 $batchResult = $conn->exec();
198 if ( $batchResult ===
false ) {
199 $this->
logRequest(
'get', implode(
',', $batchKeys ), $server,
true );
203 foreach ( $batchResult as $i =>
$blob ) {
204 if (
$blob !==
false ) {
205 $blobsFound[$batchKeys[$i]] =
$blob;
208 }
catch ( RedisException $e ) {
212 $this->
logRequest(
'get', implode(
',', $batchKeys ), $server, $e );
217 $valueSizesByKey = [];
218 foreach (
$keys as $key ) {
219 if ( array_key_exists( $key, $blobsFound ) ) {
220 $blob = $blobsFound[$key];
222 if ( $value !==
false ) {
223 $result[$key] = $value;
225 $valueSize = strlen(
$blob );
229 $valueSizesByKey[$key] = [
null, $valueSize ];
232 $this->
updateOpStats( self::METRIC_OP_GET, $valueSizesByKey );
237 protected function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
243 foreach ( $data as $key => $value ) {
246 $server = $conn->getServer();
247 $conns[$server] = $conn;
248 $batches[$server][] = $key;
255 $op = $ttl ?
'setex' :
'set';
257 $valueSizesByKey = [];
258 foreach ( $batches as $server => $batchKeys ) {
259 $conn = $conns[$server];
264 $conn->multi( Redis::PIPELINE );
265 foreach ( $batchKeys as $key ) {
272 $valueSizesByKey[$key] = [ strlen(
$serialized ), null ];
274 $batchResult = $conn->exec();
275 if ( $batchResult ===
false ) {
277 $this->
logRequest( $op, implode(
',', $batchKeys ), $server,
true );
281 $result = $result && !in_array(
false, $batchResult,
true );
282 }
catch ( RedisException $e ) {
287 $this->
logRequest( $op, implode(
',', $batchKeys ), $server, $e );
290 $this->
updateOpStats( self::METRIC_OP_SET, $valueSizesByKey );
301 foreach (
$keys as $key ) {
304 $server = $conn->getServer();
305 $conns[$server] = $conn;
306 $batches[$server][] = $key;
312 foreach ( $batches as $server => $batchKeys ) {
313 $conn = $conns[$server];
318 $conn->multi( Redis::PIPELINE );
319 foreach ( $batchKeys as $key ) {
322 $batchResult = $conn->exec();
323 if ( $batchResult ===
false ) {
325 $this->
logRequest(
'delete', implode(
',', $batchKeys ), $server,
true );
329 $result = $result && !in_array(
false, $batchResult,
true );
330 }
catch ( RedisException $e ) {
335 $this->
logRequest(
'delete', implode(
',', $batchKeys ), $server, $e );
349 foreach (
$keys as $key ) {
352 $server = $conn->getServer();
353 $conns[$server] = $conn;
354 $batches[$server][] = $key;
361 $op = ( $exptime == self::TTL_INDEFINITE )
363 : ( $relative ?
'expire' :
'expireAt' );
365 foreach ( $batches as $server => $batchKeys ) {
366 $conn = $conns[$server];
370 $conn->multi( Redis::PIPELINE );
371 foreach ( $batchKeys as $key ) {
372 if ( $exptime == self::TTL_INDEFINITE ) {
373 $conn->persist( $key );
374 } elseif ( $relative ) {
380 $batchResult = $conn->exec();
381 if ( $batchResult ===
false ) {
383 $this->
logRequest( $op, implode(
',', $batchKeys ), $server,
true );
386 $result = in_array(
false, $batchResult,
true ) ? false : $result;
387 }
catch ( RedisException $e ) {
392 $this->
logRequest( $op, implode(
',', $batchKeys ), $server, $e );
400 protected function doAdd( $key, $value, $expiry = 0, $flags = 0 ) {
411 $result = $conn->set(
414 $ttl ? [
'nx',
'ex' => $ttl ] : [
'nx' ]
416 }
catch ( RedisException $e ) {
421 $this->
logRequest(
'add', $key, $conn->getServer(), $result );
423 $this->
updateOpStats( self::METRIC_OP_ADD, [ $key => [ $valueSize,
null ] ] );
428 public function incr( $key, $value = 1, $flags = 0 ) {
435 if ( !$conn->exists( $key ) ) {
439 $result = $conn->incrBy( $key, $value );
440 }
catch ( RedisException $e ) {
445 $this->
logRequest(
'incr', $key, $conn->getServer(), $result );
452 public function decr( $key, $value = 1, $flags = 0 ) {
459 if ( !$conn->exists( $key ) ) {
463 $result = $conn->decrBy( $key, $value );
464 }
catch ( RedisException $e ) {
469 $this->
logRequest(
'decr', $key, $conn->getServer(), $result );
484 if ( $exptime == self::TTL_INDEFINITE ) {
485 $result = $conn->persist( $key );
486 $this->
logRequest(
'persist', $key, $conn->getServer(), $result );
487 } elseif ( $relative ) {
489 $this->
logRequest(
'expire', $key, $conn->getServer(), $result );
492 $this->
logRequest(
'expireAt', $key, $conn->getServer(), $result );
494 }
catch ( RedisException $e ) {
499 $this->
updateOpStats( self::METRIC_OP_CHANGE_TTL, [ $key ] );
509 $candidates = array_keys( $this->serverTagMap );
511 if ( count( $this->servers ) > 1 ) {
512 ArrayUtils::consistentHashSort( $candidates, $key,
'/' );
513 if ( !$this->automaticFailover ) {
514 $candidates = array_slice( $candidates, 0, 1 );
518 while ( ( $tag = array_shift( $candidates ) ) !==
null ) {
519 $server = $this->serverTagMap[$tag];
520 $conn = $this->redisPool->getConnection( $server, $this->logger );
530 if ( $this->automaticFailover && $candidates ) {
533 $info = $conn->info();
534 if ( ( $info[
'master_link_status'] ??
null ) ===
'down' ) {
541 }
catch ( RedisException $e ) {
543 $this->redisPool->handleError( $conn, $e );
561 $this->logger->error(
"Redis error: $msg" );
574 $this->redisPool->handleError( $conn, $e );
585 $this->
debug(
"$op($keys) on $server: " . ( $e ?
"failure" :
"success" ) );
genericKeyFromComponents(... $components)
At a minimum, there must be a keyspace and collection name component.
string $keyspace
Default keyspace; used by makeKey()
Storage medium specific cache for storing items (e.g.
getExpirationAsTimestamp( $exptime)
Convert an optionally relative timestamp to an absolute time.
getSerialized( $value, $key)
Get the serialized form a value, using any applicable prepared value.
updateOpStats(string $op, array $keyInfo)
getExpirationAsTTL( $exptime)
Convert an optionally absolute expiry time to a relative time.
isRelativeExpiration( $exptime)
setLastError( $err)
Set the "last error" registry.
Redis-based caching module for redis server >= 2.6.12 and phpredis >= 2.2.4.
RedisConnectionPool $redisPool
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
handleException(RedisConnRef $conn, RedisException $e)
The redis extension throws an exception in response to various read, write and protocol errors.
convertGenericKey( $key)
Convert a "generic" reversible cache key into one for this cache.
array $servers
List of server names.
doSetMulti(array $data, $exptime=0, $flags=0)
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
doChangeTTL( $key, $exptime, $flags)
logRequest( $op, $keys, $server, $e=null)
Send information about a single request to the debug log.
doDelete( $key, $flags=0)
Delete an item.
logError( $msg)
Log a fatal error.
doAdd( $key, $value, $expiry=0, $flags=0)
Insert an item if it does not already exist.
__construct( $params)
Construct a RedisBagOStuff object.
doGet( $key, $flags=0, &$casToken=null)
array $serverTagMap
Map of (tag => server name)
doGetMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
doChangeTTLMulti(array $keys, $exptime, $flags=0)
doSet( $key, $value, $exptime=0, $flags=0)
Set an item.
doDeleteMulti(array $keys, $flags=0)
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
Helper class to handle automatically marking connectons as reusable (via RAII pattern)
Helper class to manage Redis connections.
static singleton(array $options)
foreach( $res as $row) $serialized