Fixed Redis::select is not work expected.

This commit is contained in:
李铭昕 2019-07-02 16:41:28 +08:00
parent 83b578e4e1
commit cde44c380b
3 changed files with 31 additions and 0 deletions

View File

@ -45,6 +45,9 @@ class Redis
// Release connection.
if (! $hasContextConnection) {
if ($this->shouldUseSameConnection($name)) {
if ($name == 'select') {
$connection->setDbChanged(true);
}
// Should storage the connection to coroutine context, then use defer() to release the connection.
Context::set($this->getContextKey(), $connection);
defer(function () use ($connection) {
@ -69,6 +72,7 @@ class Redis
return in_array($methodName, [
'multi',
'pipeline',
'select',
]);
}

View File

@ -30,6 +30,11 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
*/
protected $config;
/**
* @var bool
*/
protected $dbChanged;
public function __construct(ContainerInterface $container, Pool $pool, array $config)
{
parent::__construct($container, $pool);
@ -86,4 +91,21 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
{
return true;
}
public function release(): void
{
if ($this->dbChanged) {
// Select the origin db after execute select.
$this->select($this->config['db'] ?? 0);
}
parent::release();
}
/**
* @param bool $dbChanged
*/
public function setDbChanged(bool $dbChanged): void
{
$this->dbChanged = $dbChanged;
}
}

View File

@ -33,4 +33,9 @@ class RedisTest extends TestCase
$this->assertTrue($redis->connect('127.0.0.1', 6379, 0.0));
}
public function testRedisCommand()
{
}
}