|
39 | 39 | namespace CodeIgniter\Cache\Handlers; |
40 | 40 |
|
41 | 41 | use CodeIgniter\Cache\CacheInterface; |
| 42 | +use CodeIgniter\Exceptions\CriticalError; |
42 | 43 |
|
43 | 44 | /** |
44 | 45 | * Redis cache handler |
@@ -116,19 +117,38 @@ public function initialize() |
116 | 117 | $config = $this->config; |
117 | 118 |
|
118 | 119 | $this->redis = new \Redis(); |
119 | | - if (! $this->redis->connect($config['host'], ($config['host'][0] === '/' ? 0 : $config['port']), $config['timeout'])) |
120 | | - { |
121 | | - log_message('error', 'Cache: Redis connection failed. Check your configuration.'); |
122 | | - } |
123 | 120 |
|
124 | | - if (isset($config['password']) && ! $this->redis->auth($config['password'])) |
| 121 | + // Try to connect to Redis, if an issue occurs throw a CriticalError exception, |
| 122 | + // so that the CacheFactory can attempt to initiate the next cache handler. |
| 123 | + try |
125 | 124 | { |
126 | | - log_message('error', 'Cache: Redis authentication failed.'); |
| 125 | + // Note:: If Redis is your primary cache choice, and it is "offline", every page load will end up been delayed by the timeout duration. |
| 126 | + // I feel like some sort of temporary flag should be set, to indicate that we think Redis is "offline", allowing us to bypass the timeout for a set period of time. |
| 127 | + |
| 128 | + if (! $this->redis->connect($config['host'], ($config['host'][0] === '/' ? 0 : $config['port']), $config['timeout'])) |
| 129 | + { |
| 130 | + // Note:: I'm unsure if log_message() is necessary, however I'm not 100% comfortable removing it. |
| 131 | + log_message('error', 'Cache: Redis connection failed. Check your configuration.'); |
| 132 | + throw new CriticalError('Cache: Redis connection failed. Check your configuration.'); |
| 133 | + } |
| 134 | + |
| 135 | + if (isset($config['password']) && ! $this->redis->auth($config['password'])) |
| 136 | + { |
| 137 | + log_message('error', 'Cache: Redis authentication failed.'); |
| 138 | + throw new CriticalError('Cache: Redis authentication failed.'); |
| 139 | + } |
| 140 | + |
| 141 | + if (isset($config['database']) && ! $this->redis->select($config['database'])) |
| 142 | + { |
| 143 | + log_message('error', 'Cache: Redis select database failed.'); |
| 144 | + throw new CriticalError('Cache: Redis select database failed.'); |
| 145 | + } |
127 | 146 | } |
128 | | - |
129 | | - if (isset($config['database']) && ! $this->redis->select($config['database'])) |
| 147 | + catch (\RedisException $e) |
130 | 148 | { |
131 | | - log_message('error', 'Cache: Redis select database failed.'); |
| 149 | + // $this->redis->connect() can sometimes throw a RedisException. |
| 150 | + // We need to these to a CriticalError exception and throw it. |
| 151 | + throw new CriticalError('Cache: RedisException occurred with message (' . $e->getMessage() . ').'); |
132 | 152 | } |
133 | 153 | } |
134 | 154 |
|
|
0 commit comments