From cde44c380bcdc0f2b55db5e8b058623b98db0ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Tue, 2 Jul 2019 16:41:28 +0800 Subject: [PATCH] Fixed Redis::select is not work expected. --- src/redis/src/Redis.php | 4 ++++ src/redis/src/RedisConnection.php | 22 ++++++++++++++++++++++ src/redis/tests/RedisTest.php | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/src/redis/src/Redis.php b/src/redis/src/Redis.php index b1c6ae869..8c34a6698 100644 --- a/src/redis/src/Redis.php +++ b/src/redis/src/Redis.php @@ -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', ]); } diff --git a/src/redis/src/RedisConnection.php b/src/redis/src/RedisConnection.php index af7935500..9ca8c35ae 100644 --- a/src/redis/src/RedisConnection.php +++ b/src/redis/src/RedisConnection.php @@ -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; + } } diff --git a/src/redis/tests/RedisTest.php b/src/redis/tests/RedisTest.php index 151d0858e..cb40aace5 100644 --- a/src/redis/tests/RedisTest.php +++ b/src/redis/tests/RedisTest.php @@ -33,4 +33,9 @@ class RedisTest extends TestCase $this->assertTrue($redis->connect('127.0.0.1', 6379, 0.0)); } + + public function testRedisCommand() + { + + } }