Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / objectcache / RedisBagOStuff.php
index a9af9b1..61e6926 100644 (file)
  * @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;
@@ -60,8 +65,8 @@ class RedisBagOStuff extends BagOStuff {
         */
        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];
                        }
@@ -80,15 +85,13 @@ class RedisBagOStuff extends BagOStuff {
                }
        }
 
-       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;
@@ -99,8 +102,7 @@ class RedisBagOStuff extends BagOStuff {
                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;
@@ -122,41 +124,7 @@ class RedisBagOStuff extends BagOStuff {
                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;
@@ -175,9 +143,8 @@ class RedisBagOStuff extends BagOStuff {
        }
 
        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 ) {
@@ -186,7 +153,7 @@ class RedisBagOStuff extends BagOStuff {
                        $conns[$server] = $conn;
                        $batches[$server][] = $key;
                }
-               $result = array();
+               $result = [];
                foreach ( $batches as $server => $batchKeys ) {
                        $conn = $conns[$server];
                        try {
@@ -220,9 +187,8 @@ class RedisBagOStuff extends BagOStuff {
         * @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 ) {
@@ -265,7 +231,6 @@ class RedisBagOStuff extends BagOStuff {
        }
 
        public function add( $key, $value, $expiry = 0 ) {
-
                list( $server, $conn ) = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
@@ -273,11 +238,11 @@ class RedisBagOStuff extends BagOStuff {
                $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 ) );
                        }
@@ -303,7 +268,6 @@ class RedisBagOStuff extends BagOStuff {
         * @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;
@@ -323,14 +287,6 @@ class RedisBagOStuff extends BagOStuff {
                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
@@ -381,7 +337,9 @@ class RedisBagOStuff extends BagOStuff {
 
                        // 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' ) {
@@ -398,12 +356,12 @@ class RedisBagOStuff extends BagOStuff {
                                }
                        }
 
-                       return array( $server, $conn );
+                       return [ $server, $conn ];
                }
 
                $this->setLastError( BagOStuff::ERR_UNREACHABLE );
 
-               return array( false, false );
+               return [ false, false ];
        }
 
        /**