MDL-65941 cache: Prevent cache config failure on redis problems.
authorMichael Aherne <michael.aherne@strath.ac.uk>
Mon, 17 Jun 2019 11:24:39 +0000 (12:24 +0100)
committerJake Dallimore <jake@moodle.com>
Wed, 11 Nov 2020 06:49:15 +0000 (14:49 +0800)
cache/stores/redis/lib.php

index bde6b9e..9a6b057 100644 (file)
@@ -182,22 +182,28 @@ class cachestore_redis extends cache_store implements cache_is_key_aware, cache_
             $server = $serverconf[0];
             $port = $serverconf[1];
         }
-        if ($redis->connect($server, $port)) {
-            if (!empty($password)) {
-                $redis->auth($password);
-            }
-            // If using compressor, serialisation will be done at cachestore level, not php-redis.
-            if ($this->compressor == self::COMPRESSOR_NONE) {
-                $redis->setOption(Redis::OPT_SERIALIZER, $this->serializer);
-            }
-            if (!empty($prefix)) {
-                $redis->setOption(Redis::OPT_PREFIX, $prefix);
+
+        try {
+            if ($redis->connect($server, $port)) {
+                if (!empty($password)) {
+                    $redis->auth($password);
+                }
+                // If using compressor, serialisation will be done at cachestore level, not php-redis.
+                if ($this->compressor == self::COMPRESSOR_NONE) {
+                    $redis->setOption(Redis::OPT_SERIALIZER, $this->serializer);
+                }
+                if (!empty($prefix)) {
+                    $redis->setOption(Redis::OPT_PREFIX, $prefix);
+                }
+                // Database setting option...
+                $this->isready = $this->ping($redis);
+            } else {
+                $this->isready = false;
             }
-            // Database setting option...
-            $this->isready = $this->ping($redis);
-        } else {
+        } catch (\RedisException $e) {
             $this->isready = false;
         }
+
         return $redis;
     }