diff --git a/pkg/redis/PhpRedis.php b/pkg/redis/PhpRedis.php index 139fa94dd..71ead40d4 100644 --- a/pkg/redis/PhpRedis.php +++ b/pkg/redis/PhpRedis.php @@ -26,6 +26,7 @@ public function __construct(array $config) 'reserved' => null, 'retry_interval' => null, 'persisted' => false, + 'database' => 0, ], $config); } @@ -80,6 +81,8 @@ public function connect() $this->config['retry_interval'] ); } + + $this->redis->select($this->config['database']); } return $this->redis; diff --git a/pkg/redis/RedisConnectionFactory.php b/pkg/redis/RedisConnectionFactory.php index 122bf0bef..021a448f4 100644 --- a/pkg/redis/RedisConnectionFactory.php +++ b/pkg/redis/RedisConnectionFactory.php @@ -27,6 +27,7 @@ class RedisConnectionFactory implements PsrConnectionFactory * 'vendor' => 'The library used internally to interact with Redis server * 'persisted' => bool, Whether it use single persisted connection or open a new one for every context * 'lazy' => the connection will be performed as later as possible, if the option set to true + * 'database' => Database index to select when connected (default value: 0) * ]. * * or @@ -139,6 +140,7 @@ private function defaultConfig() 'vendor' => 'phpredis', 'persisted' => false, 'lazy' => true, + 'database' => 0, ]; } } diff --git a/pkg/redis/Symfony/RedisTransportFactory.php b/pkg/redis/Symfony/RedisTransportFactory.php index 092e3baf8..f24df84e4 100644 --- a/pkg/redis/Symfony/RedisTransportFactory.php +++ b/pkg/redis/Symfony/RedisTransportFactory.php @@ -54,6 +54,10 @@ public function addConfiguration(ArrayNodeDefinition $builder) ->defaultTrue() ->info('the connection will be performed as later as possible, if the option set to true') ->end() + ->integerNode('database') + ->defaultValue(0) + ->info('Database index to select when connected.') + ->end() ; } diff --git a/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php b/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php index f3375e0de..51c185b9b 100644 --- a/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php +++ b/pkg/redis/Tests/RedisConnectionFactoryConfigTest.php @@ -71,6 +71,7 @@ public static function provideConfigs() 'vendor' => 'phpredis', 'persisted' => false, 'lazy' => true, + 'database' => 0, ], ]; @@ -85,6 +86,7 @@ public static function provideConfigs() 'vendor' => 'phpredis', 'persisted' => false, 'lazy' => true, + 'database' => 0, ], ]; @@ -99,6 +101,7 @@ public static function provideConfigs() 'vendor' => 'phpredis', 'persisted' => false, 'lazy' => true, + 'database' => 0, ], ]; @@ -114,6 +117,7 @@ public static function provideConfigs() 'persisted' => true, 'lazy' => false, 'foo' => 'bar', + 'database' => 0, ], ]; @@ -129,6 +133,7 @@ public static function provideConfigs() 'persisted' => false, 'lazy' => true, 'foo' => 'bar', + 'database' => 0, ], ]; } diff --git a/pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php b/pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php index 1b4175e6a..ca7d402c8 100644 --- a/pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php +++ b/pkg/redis/Tests/Symfony/RedisTransportFactoryTest.php @@ -58,6 +58,7 @@ public function testShouldAllowAddConfiguration() 'vendor' => 'phpredis', 'persisted' => true, 'lazy' => false, + 'database' => 0, ], $config); }