89 if ( !class_exists( Redis::class ) ) {
90 throw new RuntimeException(
91 __CLASS__ .
' requires a Redis client library. ' .
92 'See https://www.mediawiki.org/wiki/Redis#Setup' );
94 $this->logger = $options[
'logger'] ??
new NullLogger();
95 $this->connectTimeout = $options[
'connectTimeout'];
96 $this->readTimeout = $options[
'readTimeout'];
97 $this->persistent = $options[
'persistent'];
98 $this->password = $options[
'password'];
99 if ( !isset( $options[
'serializer'] ) || $options[
'serializer'] ===
'php' ) {
100 $this->serializer = Redis::SERIALIZER_PHP;
101 } elseif ( $options[
'serializer'] ===
'igbinary' ) {
102 if ( !defined(
'Redis::SERIALIZER_IGBINARY' ) ) {
103 throw new InvalidArgumentException(
104 __CLASS__ .
': configured serializer "igbinary" not available' );
106 $this->serializer = Redis::SERIALIZER_IGBINARY;
107 } elseif ( $options[
'serializer'] ===
'none' ) {
108 $this->serializer = Redis::SERIALIZER_NONE;
110 throw new InvalidArgumentException(
"Invalid serializer specified." );
193 if ( isset( $this->downServers[$server] ) ) {
195 if ( $now > $this->downServers[$server] ) {
197 unset( $this->downServers[$server] );
201 'Server "{redis_server}" is marked down for another ' .
202 ( $this->downServers[$server] - $now ) .
' seconds',
203 [
'redis_server' => $server ]
211 if ( isset( $this->connections[$server] ) ) {
212 foreach ( $this->connections[$server] as &$connection ) {
213 if ( $connection[
'free'] ) {
214 $connection[
'free'] =
false;
218 $this, $server, $connection[
'conn'],
$logger
225 throw new InvalidArgumentException(
226 __CLASS__ .
": invalid configured server \"$server\"" );
227 } elseif ( substr( $server, 0, 1 ) ===
'/' ) {
235 if ( preg_match(
'/^\[(.+)\]:(\d+)$/', $server, $m ) ) {
237 [ $host, $port ] = [ $m[1], (int)$m[2] ];
238 } elseif ( preg_match(
'/^((?:[\w]+\:\/\/)?[^:]+):(\d+)$/', $server, $m ) ) {
240 [ $host, $port ] = [ $m[1], (int)$m[2] ];
242 substr( $host, 0, 6 ) ===
'tls://'
243 && version_compare( phpversion(
'redis' ),
'5.0.0' ) < 0
245 throw new RuntimeException(
246 'A newer version of the Redis client library is required to use TLS. ' .
247 'See https://www.mediawiki.org/wiki/Redis#Setup' );
251 [ $host, $port ] = [ $server, 6379 ];
257 if ( $this->persistent ) {
258 $result = $conn->pconnect( $host, $port, $this->connectTimeout, $this->
id );
260 $result = $conn->connect( $host, $port, $this->connectTimeout );
264 'Could not connect to server "{redis_server}"',
265 [
'redis_server' => $server ]
268 $this->downServers[$server] = time() + self::SERVER_DOWN_TTL;
272 if ( ( $this->password !==
null ) && !$conn->auth( $this->password ) ) {
274 'Authentication error connecting to "{redis_server}"',
275 [
'redis_server' => $server ]
278 }
catch ( RedisException $e ) {
279 $this->downServers[$server] = time() + self::SERVER_DOWN_TTL;
281 'Redis exception connecting to "{redis_server}"',
283 'redis_server' => $server,
291 $conn->setOption( Redis::OPT_READ_TIMEOUT, $this->readTimeout );
292 $conn->setOption( Redis::OPT_SERIALIZER, $this->serializer );
293 $this->connections[$server][] = [
'conn' => $conn,
'free' => false ];
325 if ( $this->idlePoolSize <= count( $this->connections ) ) {
329 foreach ( $this->connections as &$serverConnections ) {
330 foreach ( $serverConnections as $key => &$connection ) {
331 if ( $connection[
'free'] ) {
332 unset( $serverConnections[$key] );
333 if ( --$this->idlePoolSize <= count( $this->connections ) ) {