From 646fd822c5e35485af23aa5319e3f34559a5a799 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Fri, 8 Mar 2019 22:42:48 +0000 Subject: [PATCH] Updated redis config logic, Now takes a password - Previous config did not use multiple servers in any way. - Cluster will now be created automatically if multiple servers given. - Removed REDIS_CLUSTER option. Closes #1283 --- .env.example.complete | 6 ++++++ config/database.php | 36 ++++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.env.example.complete b/.env.example.complete index 8851bd268..77a0508dc 100644 --- a/.env.example.complete +++ b/.env.example.complete @@ -75,6 +75,12 @@ CACHE_PREFIX=bookstack # For multiple servers separate with a comma MEMCACHED_SERVERS=127.0.0.1:11211:100 +# Redis server configuration +# This follows the following format: HOST:PORT:DATABASE +# or, if using a password: HOST:PORT:DATABASE:PASSWORD +# For multiple servers separate with a comma. These will be clustered. +REDIS_SERVERS=127.0.0.1:6379:0 + # Queue driver to use # Queue not really currently used but may be configurable in the future. # Would advise not to change this for now. diff --git a/config/database.php b/config/database.php index 6ca902944..93a44854f 100644 --- a/config/database.php +++ b/config/database.php @@ -8,23 +8,39 @@ * Do not edit this file unless you're happy to maintain any changes yourself. */ -// REDIS - Split out configuration into an array +// REDIS +// Split out configuration into an array if (env('REDIS_SERVERS', false)) { - $redisServerKeys = ['host', 'port', 'database']; + + $redisDefaults = ['host' => '127.0.0.1', 'port' => '6379', 'database' => '0', 'password' => null]; $redisServers = explode(',', trim(env('REDIS_SERVERS', '127.0.0.1:6379:0'), ',')); - $redisConfig = [ - 'cluster' => env('REDIS_CLUSTER', false) - ]; + $redisConfig = []; + $cluster = count($redisServers) > 1; + + if ($cluster) { + $redisConfig['clusters'] = ['default' => []]; + } + foreach ($redisServers as $index => $redisServer) { - $redisServerName = ($index === 0) ? 'default' : 'redis-server-' . $index; $redisServerDetails = explode(':', $redisServer); - if (count($redisServerDetails) < 2) $redisServerDetails[] = '6379'; - if (count($redisServerDetails) < 3) $redisServerDetails[] = '0'; - $redisConfig[$redisServerName] = array_combine($redisServerKeys, $redisServerDetails); + + $serverConfig = []; + $configIndex = 0; + foreach ($redisDefaults as $configKey => $configDefault) { + $serverConfig[$configKey] = ($redisServerDetails[$configIndex] ?? $configDefault); + $configIndex++; + } + + if ($cluster) { + $redisConfig['clusters']['default'][] = $serverConfig; + } else { + $redisConfig['default'] = $serverConfig; + } } } -// MYSQL - Split out port from host if set +// MYSQL +// Split out port from host if set $mysql_host = env('DB_HOST', 'localhost'); $mysql_host_exploded = explode(':', $mysql_host); $mysql_port = env('DB_PORT', 3306);