diff --git a/cache/stores/redis/addinstanceform.php b/cache/stores/redis/addinstanceform.php index cdd9ed653a34a..db51ab62ea5f3 100644 --- a/cache/stores/redis/addinstanceform.php +++ b/cache/stores/redis/addinstanceform.php @@ -72,5 +72,10 @@ protected function configuration_definition() { $form->addHelpButton('compressor', 'usecompressor', 'cachestore_redis'); $form->setDefault('compressor', cachestore_redis::COMPRESSOR_NONE); $form->setType('compressor', PARAM_INT); + + $form->addElement('text', 'connectiontimeout', get_string('connectiontimeout', 'cachestore_redis')); + $form->addHelpButton('connectiontimeout', 'connectiontimeout', 'cachestore_redis'); + $form->setDefault('connectiontimeout', cachestore_redis::CONNECTION_TIMEOUT); + $form->setType('connectiontimeout', PARAM_INT); } } diff --git a/cache/stores/redis/lang/en/cachestore_redis.php b/cache/stores/redis/lang/en/cachestore_redis.php index 7b6cddb30c9c8..5402ce4b89cdd 100644 --- a/cache/stores/redis/lang/en/cachestore_redis.php +++ b/cache/stores/redis/lang/en/cachestore_redis.php @@ -27,6 +27,8 @@ $string['compressor_none'] = 'No compression.'; $string['compressor_php_gzip'] = 'Use gzip compression.'; $string['compressor_php_zstd'] = 'Use Zstandard compression.'; +$string['connectiontimeout'] = 'Connection timeout'; +$string['connectiontimeout_help'] = 'This sets the timeout when attempting to connect to the Redis server.'; $string['encrypt_connection'] = 'Use TLS encryption.'; $string['encrypt_connection_help'] = 'Use TLS to connect to Redis. Do not use \'tls://\' in the hostname for Redis, use this option instead.'; $string['ca_file'] = 'CA file path'; diff --git a/cache/stores/redis/lib.php b/cache/stores/redis/lib.php index ebb5a1df2b655..d27303fc31cd1 100644 --- a/cache/stores/redis/lib.php +++ b/cache/stores/redis/lib.php @@ -64,7 +64,7 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_ const TTL_EXPIRE_BATCH = 10000; /** @var int The number of seconds to wait for a connection or response from the Redis server. */ - const CONNECTION_TIMEOUT = 10; + const CONNECTION_TIMEOUT = 3; /** * Name of this store. @@ -115,6 +115,14 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_ */ protected $compressor = self::COMPRESSOR_NONE; + + /** + * The number of seconds to wait for a connection or response from the Redis server. + * + * @var int + */ + protected $connectiontimeout = self::CONNECTION_TIMEOUT; + /** * Bytes read or written by last call to set()/get() or set_many()/get_many(). * @@ -192,6 +200,9 @@ public function __construct($name, array $configuration = array()) { if (array_key_exists('compressor', $configuration)) { $this->compressor = (int)$configuration['compressor']; } + if (array_key_exists('connectiontimeout', $configuration)) { + $this->connectiontimeout = (int)$configuration['connectiontimeout']; + } if (array_key_exists('lockwait', $configuration)) { $this->lockwait = (int)$configuration['lockwait']; } @@ -247,10 +258,10 @@ protected function new_redis(array $configuration): \Redis { $connection = $redis->connect( $server, $port, - self::CONNECTION_TIMEOUT, // Timeout. + $this->connectiontimeout, // Timeout. null, 100, // Retry interval. - self::CONNECTION_TIMEOUT, // Read timeout. + $this->connectiontimeout, // Read timeout. $opts, ); if ($connection) { @@ -797,6 +808,7 @@ public static function config_get_configuration_array($data) { 'password' => $data->password, 'serializer' => $data->serializer, 'compressor' => $data->compressor, + 'connectiontimeout' => $data->connectiontimeout, 'encryption' => $data->encryption, 'cafile' => $data->cafile, ); @@ -820,6 +832,9 @@ public static function config_set_edit_form_data(moodleform $editform, array $co if (!empty($config['compressor'])) { $data['compressor'] = $config['compressor']; } + if (!empty($config['connectiontimeout'])) { + $data['connectiontimeout'] = $config['connectiontimeout']; + } if (!empty($config['encryption'])) { $data['encryption'] = $config['encryption']; }