mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-04 04:37:46 +08:00
Merge pull request #110 from limingxinleo/redis_select
Fixed Redis::select is not work expected.
This commit is contained in:
commit
7320a3146a
@ -1,5 +1,8 @@
|
||||
# v1.0.4 - TBD
|
||||
|
||||
# Fixed
|
||||
|
||||
- [#110](https://github.com/hyperf-cloud/hyperf/pull/110) Fixed Redis::select is not work expected.
|
||||
|
||||
# v1.0.3 - 2019-07-02
|
||||
|
||||
|
@ -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',
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace HyperfTest\Redis;
|
||||
|
||||
use Hyperf\Config\Config;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Di\Container;
|
||||
use Hyperf\Redis\Pool\PoolFactory;
|
||||
use Hyperf\Redis\Pool\RedisPool;
|
||||
use Hyperf\Redis\Redis;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Hyperf\Utils\Context;
|
||||
use HyperfTest\Redis\Stub\RedisPoolStub;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
@ -20,6 +30,11 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
class RedisTest extends TestCase
|
||||
{
|
||||
public function tearDown()
|
||||
{
|
||||
Mockery::close();
|
||||
}
|
||||
|
||||
public function testRedisConnect()
|
||||
{
|
||||
$redis = new \Redis();
|
||||
@ -33,4 +48,34 @@ class RedisTest extends TestCase
|
||||
|
||||
$this->assertTrue($redis->connect('127.0.0.1', 6379, 0.0));
|
||||
}
|
||||
|
||||
public function testRedisCommand()
|
||||
{
|
||||
$container = Mockery::mock(Container::class);
|
||||
$container->shouldReceive('get')->once()->with(ConfigInterface::class)->andReturn(new Config([
|
||||
'redis' => [
|
||||
'default' => [],
|
||||
],
|
||||
]));
|
||||
$pool = new RedisPoolStub($container, 'default');
|
||||
$container->shouldReceive('make')->once()->with(RedisPool::class, ['name' => 'default'])->andReturn($pool);
|
||||
|
||||
ApplicationContext::setContainer($container);
|
||||
|
||||
$factory = new PoolFactory($container);
|
||||
|
||||
$redis = new Redis($factory);
|
||||
|
||||
$res = $redis->set('xxxx', 'yyyy');
|
||||
$this->assertSame('db:0 name:set argument:xxxx,yyyy', $res);
|
||||
|
||||
$redis->select(2);
|
||||
$res = $redis->get('xxxx');
|
||||
$this->assertSame('db:2 name:get argument:xxxx', $res);
|
||||
|
||||
parallel([function () use ($redis) {
|
||||
$res = $redis->get('xxxx');
|
||||
$this->assertSame('db:0 name:get argument:xxxx', $res);
|
||||
}]);
|
||||
}
|
||||
}
|
||||
|
54
src/redis/tests/Stub/RedisConnectionStub.php
Normal file
54
src/redis/tests/Stub/RedisConnectionStub.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\Redis\Stub;
|
||||
|
||||
use Hyperf\Redis\RedisConnection;
|
||||
|
||||
class RedisConnectionStub extends RedisConnection
|
||||
{
|
||||
public $host;
|
||||
|
||||
public $port;
|
||||
|
||||
public $auth;
|
||||
|
||||
public $db;
|
||||
|
||||
public $timeout;
|
||||
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
return sprintf('db:%d name:%s argument:%s', $this->db, $name, implode(',', $arguments));
|
||||
}
|
||||
|
||||
public function reconnect(): bool
|
||||
{
|
||||
$this->host = $this->config['host'] ?? 'localhost';
|
||||
$this->port = $this->config['port'] ?? 6379;
|
||||
$this->auth = $this->config['auth'] ?? null;
|
||||
$this->db = $this->config['db'] ?? 0;
|
||||
$this->timeout = $this->config['timeout'] ?? 0.0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getConnection()
|
||||
{
|
||||
return parent::getConnection();
|
||||
}
|
||||
|
||||
public function select($db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
}
|
24
src/redis/tests/Stub/RedisPoolStub.php
Normal file
24
src/redis/tests/Stub/RedisPoolStub.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\Redis\Stub;
|
||||
|
||||
use Hyperf\Contract\ConnectionInterface;
|
||||
use Hyperf\Redis\Pool\RedisPool;
|
||||
|
||||
class RedisPoolStub extends RedisPool
|
||||
{
|
||||
protected function createConnection(): ConnectionInterface
|
||||
{
|
||||
return new RedisConnectionStub($this->container, $this, $this->config);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user