diff --git a/module/Common/src/Cache/RedisFactory.php b/module/Common/src/Cache/RedisFactory.php index 6e7c864b..42118767 100644 --- a/module/Common/src/Cache/RedisFactory.php +++ b/module/Common/src/Cache/RedisFactory.php @@ -6,9 +6,8 @@ namespace Shlinkio\Shlink\Common\Cache; use Predis\Client as PredisClient; use Psr\Container\ContainerInterface; -use function array_shift; use function count; -use function is_array; +use function explode; use function is_string; class RedisFactory @@ -18,13 +17,11 @@ class RedisFactory public function __invoke(ContainerInterface $container): PredisClient { $redisConfig = $container->get('config')['redis'] ?? []; + $servers = $redisConfig['servers'] ?? []; + $servers = is_string($servers) ? explode(',', $servers) : $servers; + $options = count($servers) <= 1 ? null : ['cluster' => 'redis']; - if (is_array($servers) && count($servers) === 1) { - $servers = array_shift($servers); - } - - $options = is_string($servers) || count($servers) < 1 ? null : ['cluster' => 'redis']; return new PredisClient($servers, $options); } } diff --git a/module/Common/test/Cache/RedisFactoryTest.php b/module/Common/test/Cache/RedisFactoryTest.php index 765a3e8f..dbc1c9b8 100644 --- a/module/Common/test/Cache/RedisFactoryTest.php +++ b/module/Common/test/Cache/RedisFactoryTest.php @@ -54,5 +54,8 @@ class RedisFactoryTest extends TestCase yield 'empty cluster of servers' => [[ 'servers' => [], ], PredisCluster::class]; + yield 'cluster of servers as string' => [[ + 'servers' => 'tcp://1.1.1.1:6379,tcp://2.2.2.2:6379', + ], RedisCluster::class]; } }