72 $redisConf = [
'serializer' =>
'none' ];
73 foreach ( [
'connectTimeout',
'persistent',
'password' ] as
$opt ) {
81 foreach ( $this->
servers as $key => $server ) {
82 $this->serverTagMap[is_int( $key ) ? $server : $key] = $server;
85 if ( isset(
$params[
'automaticFailover'] ) ) {
86 $this->automaticFailover =
$params[
'automaticFailover'];
88 $this->automaticFailover =
true;
94 protected function doGet( $key, $flags = 0 ) {
100 $value = $conn->get( $key );
102 }
catch ( RedisException
$e ) {
107 $this->
logRequest(
'get', $key, $server, $result );
111 public function set( $key,
$value, $expiry = 0, $flags = 0 ) {
119 $result = $conn->setex( $key, $expiry, $this->
serialize( $value ) );
122 $result = $conn->set( $key, $this->
serialize( $value ) );
124 }
catch ( RedisException
$e ) {
129 $this->
logRequest(
'set', $key, $server, $result );
133 public function delete( $key ) {
142 }
catch ( RedisException
$e ) {
147 $this->
logRequest(
'delete', $key, $server, $result );
154 foreach (
$keys as $key ) {
159 $conns[$server] = $conn;
160 $batches[$server][] = $key;
163 foreach ( $batches as $server => $batchKeys ) {
164 $conn = $conns[$server];
166 $conn->multi( Redis::PIPELINE );
167 foreach ( $batchKeys as $key ) {
170 $batchResult = $conn->exec();
171 if ( $batchResult ===
false ) {
172 $this->
debug(
"multi request to $server failed" );
175 foreach ( $batchResult as $i =>
$value ) {
177 $result[$batchKeys[$i]] = $this->
unserialize( $value );
180 }
catch ( RedisException
$e ) {
185 $this->
debug(
"getMulti for " . count(
$keys ) .
" keys " .
186 "returned " . count( $result ) .
" results" );
195 public function setMulti( array $data, $expiry = 0 ) {
198 foreach ( $data as $key =>
$value ) {
203 $conns[$server] = $conn;
204 $batches[$server][] = $key;
209 foreach ( $batches as $server => $batchKeys ) {
210 $conn = $conns[$server];
212 $conn->multi( Redis::PIPELINE );
213 foreach ( $batchKeys as $key ) {
215 $conn->setex( $key, $expiry, $this->
serialize( $data[$key] ) );
217 $conn->set( $key, $this->
serialize( $data[$key] ) );
220 $batchResult = $conn->exec();
221 if ( $batchResult ===
false ) {
222 $this->
debug(
"setMulti request to $server failed" );
225 foreach ( $batchResult as
$value ) {
230 }
catch ( RedisException
$e ) {
247 $result = $conn->set(
250 [
'nx',
'ex' => $expiry ]
253 $result = $conn->setnx( $key, $this->
serialize( $value ) );
255 }
catch ( RedisException
$e ) {
260 $this->
logRequest(
'add', $key, $server, $result );
282 if ( !$conn->exists( $key ) ) {
286 $result = $conn->incrBy( $key,
$value );
287 }
catch ( RedisException
$e ) {
292 $this->
logRequest(
'incr', $key, $server, $result );
304 $result = $conn->expire( $key, $expiry );
305 }
catch ( RedisException
$e ) {
310 $this->
logRequest(
'expire', $key, $server, $result );
315 if ( array_key_exists(
'val', $event ) ) {
316 $event[
'val'] =
serialize( $event[
'val'] );
329 return is_int( $data ) ? $data :
serialize( $data );
337 $int = intval( $data );
347 $candidates = array_keys( $this->serverTagMap );
349 if ( count( $this->
servers ) > 1 ) {
350 ArrayUtils::consistentHashSort( $candidates, $key,
'/' );
351 if ( !$this->automaticFailover ) {
352 $candidates = array_slice( $candidates, 0, 1 );
356 while ( ( $tag = array_shift( $candidates ) ) !==
null ) {
357 $server = $this->serverTagMap[$tag];
358 $conn = $this->redisPool->getConnection( $server, $this->logger );
368 if ( $this->automaticFailover && $candidates ) {
377 }
catch ( RedisException
$e ) {
384 return [ $server, $conn ];
389 return [
false,
false ];
399 $info = $conn->info();
400 return isset( $info[
'master_link_status'] )
401 ? $info[
'master_link_status']
410 $this->logger->error(
"Redis error: $msg" );
423 $this->redisPool->handleError( $conn,
$e );
433 public function logRequest( $method, $key, $server, $result ) {
434 $this->
debug(
"$method $key on $server: " .
435 ( $result ===
false ?
"failure" :
"success" ) );
interface is intended to be more or less compatible with the PHP memcached client.
convertToRelative( $exptime)
Convert an optionally absolute expiry time to a relative time.
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)
Non-atomic implementation of incr().
modifySimpleRelayEvent(array $event)
Modify a cache update operation array for EventRelayer::notify()
getMasterLinkStatus(RedisConnRef $conn)
Check the master link status of a Redis server that is configured as a replica DB.
array $servers
List of server names.
setMulti(array $data, $expiry=0)
logError( $msg)
Log a fatal error.
getConnection( $key)
Get a Redis object with a connection suitable for fetching the specified key.
__construct( $params)
Construct a RedisBagOStuff object.
changeTTL( $key, $expiry=0)
Reset the TTL on a key if it exists.
array $serverTagMap
Map of (tag => server name)
add( $key, $value, $expiry=0)
handleException(RedisConnRef $conn, $e)
The redis extension throws an exception in response to various read, write and protocol errors.
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
logRequest( $method, $key, $server, $result)
Send information about a single request to the debug log.
Helper class to handle automatically marking connectons as reusable (via RAII pattern)
Helper class to manage Redis connections.
static singleton(array $options)
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
namespace being checked & $result
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
processing should stop and the error should be shown to the user * false
returning false will NOT prevent logging $e
const QOS_SYNCWRITES_NONE
storage can be distributed across multiple servers