Skip to content

Commit

Permalink
fix: Fixed REDIS_DB is not effected when redis reconnected. (#242)
Browse files Browse the repository at this point in the history
* Fixed REDIS_DB is not effected when redis reconnected.

* 修改切换DB操作统一在createConnection中进行

* 增加重连后,REDIS_DB会失效的BUG
  • Loading branch information
limingxinleo authored and swoft-bot committed Nov 16, 2018
1 parent 8f3f5a5 commit 29b7e81
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
4 changes: 0 additions & 4 deletions src/AbstractRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 0 additions & 3 deletions src/Pool/RedisPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public function createConnection(): ConnectionInterface
$redis = new SyncRedisConnection($this);
}

$dbIndex = $this->poolConfig->getDb();
$redis->select($dbIndex);

return $redis;
}
}
4 changes: 4 additions & 0 deletions src/RedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function createConnection()
{
$redis = $this->initRedis();
$this->connection = $redis;

/** @var RedisPoolConfig $config */
$config = $this->getPool()->getPoolConfig();
$redis->select($config->getDb());
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/SyncRedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
13 changes: 13 additions & 0 deletions test/Cases/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 29b7e81

Please sign in to comment.