* @file
*/
+/**
+ * Redis-based caching module for redis server >= 2.6.12
+ *
+ * @note: avoid use of Redis::MULTI transactions for twemproxy support
+ */
class RedisBagOStuff extends BagOStuff {
/** @var RedisConnectionPool */
protected $redisPool;
*/
function __construct( $params ) {
parent::__construct( $params );
- $redisConf = array( 'serializer' => 'none' ); // manage that in this class
- foreach ( array( 'connectTimeout', 'persistent', 'password' ) as $opt ) {
+ $redisConf = [ 'serializer' => 'none' ]; // manage that in this class
+ foreach ( [ 'connectTimeout', 'persistent', 'password' ] as $opt ) {
if ( isset( $params[$opt] ) ) {
$redisConf[$opt] = $params[$opt];
}
}
}
- public function get( $key, &$casToken = null, $flags = 0 ) {
-
+ protected function doGet( $key, $flags = 0 ) {
list( $server, $conn ) = $this->getConnection( $key );
if ( !$conn ) {
return false;
}
try {
$value = $conn->get( $key );
- $casToken = $value;
$result = $this->unserialize( $value );
} catch ( RedisException $e ) {
$result = false;
return $result;
}
- public function set( $key, $value, $expiry = 0 ) {
-
+ public function set( $key, $value, $expiry = 0, $flags = 0 ) {
list( $server, $conn ) = $this->getConnection( $key );
if ( !$conn ) {
return false;
return $result;
}
- protected function cas( $casToken, $key, $value, $expiry = 0 ) {
-
- list( $server, $conn ) = $this->getConnection( $key );
- if ( !$conn ) {
- return false;
- }
- $expiry = $this->convertToRelative( $expiry );
- try {
- $conn->watch( $key );
-
- if ( $this->serialize( $this->get( $key ) ) !== $casToken ) {
- $conn->unwatch();
- return false;
- }
-
- // multi()/exec() will fail atomically if the key changed since watch()
- $conn->multi();
- if ( $expiry ) {
- $conn->setex( $key, $expiry, $this->serialize( $value ) );
- } else {
- // No expiry, that is very different from zero expiry in Redis
- $conn->set( $key, $this->serialize( $value ) );
- }
- $result = ( $conn->exec() == array( true ) );
- } catch ( RedisException $e ) {
- $result = false;
- $this->handleException( $conn, $e );
- }
-
- $this->logRequest( 'cas', $key, $server, $result );
- return $result;
- }
-
public function delete( $key ) {
-
list( $server, $conn ) = $this->getConnection( $key );
if ( !$conn ) {
return false;
}
public function getMulti( array $keys, $flags = 0 ) {
-
- $batches = array();
- $conns = array();
+ $batches = [];
+ $conns = [];
foreach ( $keys as $key ) {
list( $server, $conn ) = $this->getConnection( $key );
if ( !$conn ) {
$conns[$server] = $conn;
$batches[$server][] = $key;
}
- $result = array();
+ $result = [];
foreach ( $batches as $server => $batchKeys ) {
$conn = $conns[$server];
try {
* @return bool
*/
public function setMulti( array $data, $expiry = 0 ) {
-
- $batches = array();
- $conns = array();
+ $batches = [];
+ $conns = [];
foreach ( $data as $key => $value ) {
list( $server, $conn ) = $this->getConnection( $key );
if ( !$conn ) {
}
public function add( $key, $value, $expiry = 0 ) {
-
list( $server, $conn ) = $this->getConnection( $key );
if ( !$conn ) {
return false;
$expiry = $this->convertToRelative( $expiry );
try {
if ( $expiry ) {
- $conn->multi();
- $conn->setnx( $key, $this->serialize( $value ) );
- // @FIXME: this always bumps the TTL; use Redis 2.8 or Lua
- $conn->expire( $key, $expiry );
- $result = ( $conn->exec() == array( true, true ) );
+ $result = $conn->set(
+ $key,
+ $this->serialize( $value ),
+ [ 'nx', 'ex' => $expiry ]
+ );
} else {
$result = $conn->setnx( $key, $this->serialize( $value ) );
}
* @return int|bool New value or false on failure
*/
public function incr( $key, $value = 1 ) {
-
list( $server, $conn ) = $this->getConnection( $key );
if ( !$conn ) {
return false;
return $result;
}
- public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
- if ( !is_callable( $callback ) ) {
- throw new Exception( "Got invalid callback." );
- }
-
- return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
- }
-
public function modifySimpleRelayEvent( array $event ) {
if ( array_key_exists( 'val', $event ) ) {
$event['val'] = serialize( $event['val'] ); // this class uses PHP serialization
// If automatic failover is enabled, check that the server's link
// to its master (if any) is up -- but only if there are other
- // viable candidates left to consider.
+ // viable candidates left to consider. Also, getMasterLinkStatus()
+ // does not work with twemproxy, though $candidates will be empty
+ // by now in such cases.
if ( $this->automaticFailover && $candidates ) {
try {
if ( $this->getMasterLinkStatus( $conn ) === 'down' ) {
}
}
- return array( $server, $conn );
+ return [ $server, $conn ];
}
$this->setLastError( BagOStuff::ERR_UNREACHABLE );
- return array( false, false );
+ return [ false, false ];
}
/**