diff --git a/src/AbstractRedisConnection.php b/src/AbstractRedisConnection.php index a9784f0..2706fbd 100644 --- a/src/AbstractRedisConnection.php +++ b/src/AbstractRedisConnection.php @@ -88,10 +88,6 @@ protected function initRedis() $error = sprintf('Redis connection authentication failed host=%s port=%d auth=%s', $host, (int)$port, (string)$config['auth']); throw new RedisException($error); } - if (isset($config['database']) && $config['database'] < 16 && false === $redis->select($config['database'])) { - $error = sprintf('Redis selection database failure host=%s port=%d database=%d', $host, (int)$port, (int)$config['database']); - throw new RedisException($error); - } return $redis; } diff --git a/src/Pool/RedisPool.php b/src/Pool/RedisPool.php index 14bb74a..96c2bf2 100644 --- a/src/Pool/RedisPool.php +++ b/src/Pool/RedisPool.php @@ -39,9 +39,6 @@ public function createConnection(): ConnectionInterface $redis = new SyncRedisConnection($this); } - $dbIndex = $this->poolConfig->getDb(); - $redis->select($dbIndex); - return $redis; } } diff --git a/src/RedisConnection.php b/src/RedisConnection.php index a2a5aa4..e088de3 100644 --- a/src/RedisConnection.php +++ b/src/RedisConnection.php @@ -33,6 +33,10 @@ public function createConnection() { $redis = $this->initRedis(); $this->connection = $redis; + + /** @var RedisPoolConfig $config */ + $config = $this->getPool()->getPoolConfig(); + $redis->select($config->getDb()); } /** diff --git a/src/SyncRedisConnection.php b/src/SyncRedisConnection.php index 827db2c..90c5290 100644 --- a/src/SyncRedisConnection.php +++ b/src/SyncRedisConnection.php @@ -39,6 +39,10 @@ public function createConnection() $redis->setOption(\Redis::OPT_PREFIX, $prefix); } $this->connection = $redis; + + /** @var RedisPoolConfig $config */ + $config = $this->getPool()->getPoolConfig(); + $redis->select($config->getDb()); } /** diff --git a/test/Cases/PoolTest.php b/test/Cases/PoolTest.php index a72a8cb..5a83516 100644 --- a/test/Cases/PoolTest.php +++ b/test/Cases/PoolTest.php @@ -4,6 +4,7 @@ use Swoft\App; use Swoft\Redis\Exception\RedisException; +use Swoft\Redis\Redis; use SwoftTest\Redis\Pool\RedisEnvPoolConfig; use SwoftTest\Redis\Pool\RedisPptPoolConfig; use SwoftTest\Redis\Testing\Clients\TimeoutRedis; @@ -73,4 +74,16 @@ public function testRedisTimeout() }); } + + public function testRedisReconnectSelectDb() + { + $redis = bean(Redis::class); + $redis->set('test_select_db', 1); + + $redis->reconnect(); + + $res = $redis->get('test_select_db'); + + $this->assertEquals(1, $res); + } } \ No newline at end of file