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;
88 $this->attrMap[self::ATTR_SYNCWRITES] = self::QOS_SYNCWRITES_NONE;
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->
getSerialized( $value, $key ) );
127 $result = $conn->set( $key, $this->
getSerialized( $value, $key ) );
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->
getSerialized( $data[$key], $key ) );
232 $conn->set( $key, $this->
getSerialized( $data[$key], $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;
308 $op = ( $exptime == self::TTL_INDEFINITE )
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 ) {
446 ArrayUtils::consistentHashSort( $candidates, $key,
'/' );
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" ) );
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.
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.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
handleException(RedisConnRef $conn, RedisException $e)
The redis extension throws an exception in response to various read, write and protocol errors.
array $servers
List of server names.
doSetMulti(array $data, $exptime=0, $flags=0)
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.
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)