From 7cc64739f814ea899f7e64831b02ba9461d34654 Mon Sep 17 00:00:00 2001 From: Tai Lam Date: Wed, 6 Nov 2019 12:46:11 +0800 Subject: [PATCH 1/7] Support docker cluster --- doc/zh/redis.md | 16 ++++++++++------ doc/zh/tutorial/docker-swarm.md | 1 + src/redis/publish/redis.php | 1 + src/redis/src/RedisConnection.php | 19 ++++++++++++++++--- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/doc/zh/redis.md b/doc/zh/redis.md index 58a2aa7ee..77b58f6a3 100644 --- a/doc/zh/redis.md +++ b/doc/zh/redis.md @@ -8,12 +8,13 @@ composer require hyperf/redis ## 配置 -| 配置项 | 类型 | 默认值 | 备注 | -|:------:|:-------:|:-----------:|:---------:| -| host | string | 'localhost' | Redis地址 | -| auth | string | 无 | 密码 | -| port | integer | 6379 | 端口 | -| db | integer | 0 | DB | +| 配置项 | 类型 | 默认值 | 备注 | +|:--------:|:-------:|:-----------:|:---------:| +| host | string | 'localhost' | Redis地址 | +| auth | string | 无 | 密码 | +| port | integer | 6379 | 端口 | +| cluster | boolean | false | 集群 | +| db | integer | 0 | DB | ```php env('REDIS_HOST', 'localhost'), 'auth' => env('REDIS_AUTH', ''), 'port' => (int) env('REDIS_PORT', 6379), + 'cluster' => env('REDIS_CLUSTER', false), 'db' => (int) env('REDIS_DB', 0), 'pool' => [ 'min_connections' => 1, @@ -62,6 +64,7 @@ return [ 'host' => env('REDIS_HOST', 'localhost'), 'auth' => env('REDIS_AUTH', ''), 'port' => (int) env('REDIS_PORT', 6379), + 'cluster' => env('REDIS_CLUSTER', false), 'db' => (int) env('REDIS_DB', 0), 'pool' => [ 'min_connections' => 1, @@ -77,6 +80,7 @@ return [ 'host' => env('REDIS_HOST', 'localhost'), 'auth' => env('REDIS_AUTH', ''), 'port' => (int) env('REDIS_PORT', 6379), + 'cluster' => env('REDIS_CLUSTER', false), 'db' => 1, 'pool' => [ 'min_connections' => 1, diff --git a/doc/zh/tutorial/docker-swarm.md b/doc/zh/tutorial/docker-swarm.md index 9628761f6..7c59873be 100644 --- a/doc/zh/tutorial/docker-swarm.md +++ b/doc/zh/tutorial/docker-swarm.md @@ -230,6 +230,7 @@ DB_PREFIX= REDIS_HOST=localhost REDIS_AUTH= REDIS_PORT=6379 +REDIS_CLUSTER=false REDIS_DB=0 ``` diff --git a/src/redis/publish/redis.php b/src/redis/publish/redis.php index 2386672d1..e0760a874 100644 --- a/src/redis/publish/redis.php +++ b/src/redis/publish/redis.php @@ -15,6 +15,7 @@ return [ 'host' => env('REDIS_HOST', 'localhost'), 'auth' => env('REDIS_AUTH', null), 'port' => (int) env('REDIS_PORT', 6379), + 'cluster' => env('REDIS_CLUSTER', false), 'db' => (int) env('REDIS_DB', 0), 'timeout' => 0.0, 'reserved' => null, diff --git a/src/redis/src/RedisConnection.php b/src/redis/src/RedisConnection.php index 441aa5f12..7b4c56dd0 100644 --- a/src/redis/src/RedisConnection.php +++ b/src/redis/src/RedisConnection.php @@ -32,6 +32,7 @@ class RedisConnection extends BaseConnection implements ConnectionInterface 'host' => 'localhost', 'port' => 6379, 'auth' => null, + 'cluster' => false, 'db' => 0, 'timeout' => 0.0, 'options' => [], @@ -74,12 +75,24 @@ class RedisConnection extends BaseConnection implements ConnectionInterface $host = $this->config['host']; $port = $this->config['port']; $auth = $this->config['auth']; + $cluster = $this->config['cluster']; $db = $this->config['db']; $timeout = $this->config['timeout']; - $redis = new \Redis(); - if (! $redis->connect($host, $port, $timeout)) { - throw new ConnectionException('Connection reconnect failed.'); + $redis = null; + if ($cluster !== true) { + // Normal Redis (Non-cluster) + $redis = new \Redis(); + if (! $redis->connect($host, $port, $timeout)) { + throw new ConnectionException('Connection reconnect failed.'); + } + } else { + // Redis Cluster + try { + $redis = new \RedisCluster(null, [$host . ':' . $port], $timeout); + } catch (\Throwable $e) { + throw new ConnectionException('Connection reconnect failed. ' . $e->getMessage()); + } } $options = $this->config['options'] ?? []; From 1984d4fd4d2945df68023794d2f07db0d9ce20dd Mon Sep 17 00:00:00 2001 From: Tai Lam Date: Wed, 6 Nov 2019 13:22:05 +0800 Subject: [PATCH 2/7] Support docker cluster (update test cases) --- src/redis/tests/RedisConnectionTest.php | 2 ++ src/redis/tests/RedisProxyTest.php | 1 + src/redis/tests/RedisTest.php | 1 + 3 files changed, 4 insertions(+) diff --git a/src/redis/tests/RedisConnectionTest.php b/src/redis/tests/RedisConnectionTest.php index 31b5d1189..383648371 100644 --- a/src/redis/tests/RedisConnectionTest.php +++ b/src/redis/tests/RedisConnectionTest.php @@ -45,6 +45,7 @@ class RedisConnectionTest extends TestCase 'host' => 'redis', 'port' => 16379, 'auth' => 'redis', + 'cluster' => false, 'db' => 0, 'timeout' => 0.0, 'options' => [], @@ -107,6 +108,7 @@ class RedisConnectionTest extends TestCase 'host' => 'redis', 'auth' => 'redis', 'port' => 16379, + 'cluster' => false, 'pool' => [ 'min_connections' => 1, 'max_connections' => 30, diff --git a/src/redis/tests/RedisProxyTest.php b/src/redis/tests/RedisProxyTest.php index afd10d2f3..8fa556d54 100644 --- a/src/redis/tests/RedisProxyTest.php +++ b/src/redis/tests/RedisProxyTest.php @@ -78,6 +78,7 @@ class RedisProxyTest extends TestCase 'host' => 'localhost', 'auth' => null, 'port' => 6379, + 'cluster' => false, 'db' => 0, 'options' => $optinos, 'pool' => [ diff --git a/src/redis/tests/RedisTest.php b/src/redis/tests/RedisTest.php index 40dbc8e8e..2fa6d3eb0 100644 --- a/src/redis/tests/RedisTest.php +++ b/src/redis/tests/RedisTest.php @@ -77,6 +77,7 @@ class RedisTest extends TestCase 'host' => 'localhost', 'auth' => null, 'port' => 6379, + 'cluster' => false, 'db' => 0, 'pool' => [ 'min_connections' => 1, From e690fe3b1281c127ceb938cdbfbc201937a88aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 6 Nov 2019 14:49:01 +0800 Subject: [PATCH 3/7] Optimized code. --- .travis.yml | 2 +- doc/zh/redis.md | 17 +++++++--- doc/zh/tutorial/docker-swarm.md | 1 - src/redis/publish/redis.php | 6 +++- src/redis/src/Pool/PoolFactory.php | 1 - src/redis/src/Pool/RedisPool.php | 3 -- src/redis/src/RedisConnection.php | 34 ++++++++++++++------ src/redis/src/RedisFactory.php | 2 +- src/redis/tests/RedisConnectionTest.php | 7 ++-- src/redis/tests/RedisProxyTest.php | 1 - src/redis/tests/RedisTest.php | 1 - src/redis/tests/Stub/RedisConnectionStub.php | 6 ---- 12 files changed, 50 insertions(+), 31 deletions(-) diff --git a/.travis.yml b/.travis.yml index 360004230..c49f765f9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,6 @@ before_script: - composer config -g process-timeout 900 && composer update script: - - composer analyse src/di src/json-rpc src/tracer src/metric + - composer analyse src/di src/json-rpc src/tracer src/metric src/redis - composer test -- --exclude-group NonCoroutine - vendor/bin/phpunit --group NonCoroutine diff --git a/doc/zh/redis.md b/doc/zh/redis.md index 77b58f6a3..9fd3fc1c2 100644 --- a/doc/zh/redis.md +++ b/doc/zh/redis.md @@ -13,8 +13,10 @@ composer require hyperf/redis | host | string | 'localhost' | Redis地址 | | auth | string | 无 | 密码 | | port | integer | 6379 | 端口 | -| cluster | boolean | false | 集群 | | db | integer | 0 | DB | +| cluster.enable | boolean | false | 是否集群模式 | +| cluster.name | string | null | 集群名 | +| cluster.seeds | array | [] | 是否集群模式 | ```php env('REDIS_HOST', 'localhost'), 'auth' => env('REDIS_AUTH', ''), 'port' => (int) env('REDIS_PORT', 6379), - 'cluster' => env('REDIS_CLUSTER', false), 'db' => (int) env('REDIS_DB', 0), + 'cluster' => [ + 'enable' => (bool) env('REDIS_ENABLE_CLUSTER', false), + 'name' => null, + 'seeds' => [], + ], 'pool' => [ 'min_connections' => 1, 'max_connections' => 10, @@ -64,8 +70,12 @@ return [ 'host' => env('REDIS_HOST', 'localhost'), 'auth' => env('REDIS_AUTH', ''), 'port' => (int) env('REDIS_PORT', 6379), - 'cluster' => env('REDIS_CLUSTER', false), 'db' => (int) env('REDIS_DB', 0), + 'cluster' => [ + 'enable' => (bool) env('REDIS_ENABLE_CLUSTER', false), + 'name' => null, + 'seeds' => [], + ], 'pool' => [ 'min_connections' => 1, 'max_connections' => 10, @@ -80,7 +90,6 @@ return [ 'host' => env('REDIS_HOST', 'localhost'), 'auth' => env('REDIS_AUTH', ''), 'port' => (int) env('REDIS_PORT', 6379), - 'cluster' => env('REDIS_CLUSTER', false), 'db' => 1, 'pool' => [ 'min_connections' => 1, diff --git a/doc/zh/tutorial/docker-swarm.md b/doc/zh/tutorial/docker-swarm.md index 7c59873be..9628761f6 100644 --- a/doc/zh/tutorial/docker-swarm.md +++ b/doc/zh/tutorial/docker-swarm.md @@ -230,7 +230,6 @@ DB_PREFIX= REDIS_HOST=localhost REDIS_AUTH= REDIS_PORT=6379 -REDIS_CLUSTER=false REDIS_DB=0 ``` diff --git a/src/redis/publish/redis.php b/src/redis/publish/redis.php index e0760a874..c2cdd9bec 100644 --- a/src/redis/publish/redis.php +++ b/src/redis/publish/redis.php @@ -15,11 +15,15 @@ return [ 'host' => env('REDIS_HOST', 'localhost'), 'auth' => env('REDIS_AUTH', null), 'port' => (int) env('REDIS_PORT', 6379), - 'cluster' => env('REDIS_CLUSTER', false), 'db' => (int) env('REDIS_DB', 0), 'timeout' => 0.0, 'reserved' => null, 'retry_interval' => 0, + 'cluster' => [ + 'enable' => (bool) env('REDIS_ENABLE_CLUSTER', false), + 'name' => null, + 'seeds' => [], + ], 'pool' => [ 'min_connections' => 1, 'max_connections' => 10, diff --git a/src/redis/src/Pool/PoolFactory.php b/src/redis/src/Pool/PoolFactory.php index 312d22365..f26c5fc77 100644 --- a/src/redis/src/Pool/PoolFactory.php +++ b/src/redis/src/Pool/PoolFactory.php @@ -14,7 +14,6 @@ namespace Hyperf\Redis\Pool; use Hyperf\Di\Container; use Psr\Container\ContainerInterface; -use Swoole\Coroutine\Channel; class PoolFactory { diff --git a/src/redis/src/Pool/RedisPool.php b/src/redis/src/Pool/RedisPool.php index a1efdcd05..0c0137eff 100644 --- a/src/redis/src/Pool/RedisPool.php +++ b/src/redis/src/Pool/RedisPool.php @@ -49,9 +49,6 @@ class RedisPool extends Pool parent::__construct($container, $options); } - /** - * @return string - */ public function getName(): string { return $this->name; diff --git a/src/redis/src/RedisConnection.php b/src/redis/src/RedisConnection.php index 7b4c56dd0..0cbbad07f 100644 --- a/src/redis/src/RedisConnection.php +++ b/src/redis/src/RedisConnection.php @@ -18,6 +18,9 @@ use Hyperf\Pool\Exception\ConnectionException; use Hyperf\Pool\Pool; use Psr\Container\ContainerInterface; +/** + * @method select(int $db) + */ class RedisConnection extends BaseConnection implements ConnectionInterface { /** @@ -32,9 +35,13 @@ class RedisConnection extends BaseConnection implements ConnectionInterface 'host' => 'localhost', 'port' => 6379, 'auth' => null, - 'cluster' => false, 'db' => 0, 'timeout' => 0.0, + 'cluster' => [ + 'enable' => false, + 'name' => null, + 'seeds' => [], + ], 'options' => [], ]; @@ -75,24 +82,18 @@ class RedisConnection extends BaseConnection implements ConnectionInterface $host = $this->config['host']; $port = $this->config['port']; $auth = $this->config['auth']; - $cluster = $this->config['cluster']; $db = $this->config['db']; $timeout = $this->config['timeout']; + $cluster = $this->config['cluster']['enable'] ?? false; $redis = null; if ($cluster !== true) { - // Normal Redis (Non-cluster) $redis = new \Redis(); if (! $redis->connect($host, $port, $timeout)) { throw new ConnectionException('Connection reconnect failed.'); } } else { - // Redis Cluster - try { - $redis = new \RedisCluster(null, [$host . ':' . $port], $timeout); - } catch (\Throwable $e) { - throw new ConnectionException('Connection reconnect failed. ' . $e->getMessage()); - } + $redis = $this->createRedisCluster(); } $options = $this->config['options'] ?? []; @@ -138,4 +139,19 @@ class RedisConnection extends BaseConnection implements ConnectionInterface { $this->database = $database; } + + protected function createRedisCluster() + { + try { + $seeds = $this->config['cluster']['seeds'] ?? []; + $name = $this->config['cluster']['name'] ?? null; + $timeout = $this->config['timeout'] ?? null; + + $redis = new \RedisCluster($name, $seeds, $timeout); + } catch (\Throwable $e) { + throw new ConnectionException('Connection reconnect failed. ' . $e->getMessage()); + } + + return $redis; + } } diff --git a/src/redis/src/RedisFactory.php b/src/redis/src/RedisFactory.php index 663e33439..6630b1f5a 100644 --- a/src/redis/src/RedisFactory.php +++ b/src/redis/src/RedisFactory.php @@ -32,7 +32,7 @@ class RedisFactory } /** - * @return \Redis + * @return \Redis|RedisProxy */ public function get(string $poolName) { diff --git a/src/redis/tests/RedisConnectionTest.php b/src/redis/tests/RedisConnectionTest.php index 383648371..de6e6f2a1 100644 --- a/src/redis/tests/RedisConnectionTest.php +++ b/src/redis/tests/RedisConnectionTest.php @@ -45,9 +45,13 @@ class RedisConnectionTest extends TestCase 'host' => 'redis', 'port' => 16379, 'auth' => 'redis', - 'cluster' => false, 'db' => 0, 'timeout' => 0.0, + 'cluster' => [ + 'enable' => false, + 'name' => null, + 'seeds' => [], + ], 'options' => [], 'pool' => [ 'min_connections' => 1, @@ -108,7 +112,6 @@ class RedisConnectionTest extends TestCase 'host' => 'redis', 'auth' => 'redis', 'port' => 16379, - 'cluster' => false, 'pool' => [ 'min_connections' => 1, 'max_connections' => 30, diff --git a/src/redis/tests/RedisProxyTest.php b/src/redis/tests/RedisProxyTest.php index 8fa556d54..afd10d2f3 100644 --- a/src/redis/tests/RedisProxyTest.php +++ b/src/redis/tests/RedisProxyTest.php @@ -78,7 +78,6 @@ class RedisProxyTest extends TestCase 'host' => 'localhost', 'auth' => null, 'port' => 6379, - 'cluster' => false, 'db' => 0, 'options' => $optinos, 'pool' => [ diff --git a/src/redis/tests/RedisTest.php b/src/redis/tests/RedisTest.php index 2fa6d3eb0..40dbc8e8e 100644 --- a/src/redis/tests/RedisTest.php +++ b/src/redis/tests/RedisTest.php @@ -77,7 +77,6 @@ class RedisTest extends TestCase 'host' => 'localhost', 'auth' => null, 'port' => 6379, - 'cluster' => false, 'db' => 0, 'pool' => [ 'min_connections' => 1, diff --git a/src/redis/tests/Stub/RedisConnectionStub.php b/src/redis/tests/Stub/RedisConnectionStub.php index a666e6153..8a985ad6b 100644 --- a/src/redis/tests/Stub/RedisConnectionStub.php +++ b/src/redis/tests/Stub/RedisConnectionStub.php @@ -47,17 +47,11 @@ class RedisConnectionStub extends RedisConnection $this->db = $db; } - /** - * @return array - */ public function getConfig(): array { return $this->config; } - /** - * @return null|int - */ public function getDatabase(): ?int { return $this->database; From 12004b1d1f35e1bc0f2f0380e0a9830221321970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 6 Nov 2019 15:02:01 +0800 Subject: [PATCH 4/7] Update CHANGELOG.md --- CHANGELOG.md | 1 + doc/zh/redis.md | 74 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5eea213d7..ba0dda90c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - [#832](https://github.com/hyperf/hyperf/pull/832) Added `Hyperf\Utils\Codec\Json`. - [#833](https://github.com/hyperf/hyperf/pull/833) Added `Hyperf\Utils\Backoff`. - [#852](https://github.com/hyperf/hyperf/pull/852) Added method `clear` for Parallel to clear callbacks. +- [#873](https://github.com/hyperf/hyperf/pull/873) Added redis cluster. ## Fixed diff --git a/doc/zh/redis.md b/doc/zh/redis.md index 9fd3fc1c2..94869ca5d 100644 --- a/doc/zh/redis.md +++ b/doc/zh/redis.md @@ -8,15 +8,15 @@ composer require hyperf/redis ## 配置 -| 配置项 | 类型 | 默认值 | 备注 | -|:--------:|:-------:|:-----------:|:---------:| -| host | string | 'localhost' | Redis地址 | -| auth | string | 无 | 密码 | -| port | integer | 6379 | 端口 | -| db | integer | 0 | DB | -| cluster.enable | boolean | false | 是否集群模式 | -| cluster.name | string | null | 集群名 | -| cluster.seeds | array | [] | 是否集群模式 | +| 配置项 | 类型 | 默认值 | 备注 | +|:--------------:|:-------:|:-----------:|:--------------------------------:| +| host | string | 'localhost' | Redis地址 | +| auth | string | 无 | 密码 | +| port | integer | 6379 | 端口 | +| db | integer | 0 | DB | +| cluster.enable | boolean | false | 是否集群模式 | +| cluster.name | string | null | 集群名 | +| cluster.seeds | array | [] | 集群连接地址阿数组 ['host:port'] | ```php get(RedisFactory::class)->get('foo'); $result = $redis->keys('*'); ``` +## 集群模式 + +### 使用 `name` + +配置 `cluster`,修改修改 `redis.ini`,也可以修改 `Dockerfile` 如下 + +``` + # - config PHP + && { \ + echo "upload_max_filesize=100M"; \ + echo "post_max_size=108M"; \ + echo "memory_limit=1024M"; \ + echo "date.timezone=${TIMEZONE}"; \ + echo "redis.clusters.seeds = \"mycluster[]=localhost:7000&mycluster[]=localhost:7001\""; \ + echo "redis.clusters.timeout = \"mycluster=5\""; \ + echo "redis.clusters.read_timeout = \"mycluster=10\""; \ + echo "redis.clusters.auth = \"mycluster=password\""; + } | tee conf.d/99-overrides.ini \ +``` + +对应 PHP 配置如下 + +```php + [ + 'cluster' => [ + 'enable' => true, + 'name' => 'mycluster', + 'seeds' => [], + ], + ], +]; +``` + +### 使用 seeds + +当然不配置 name 直接使用 seeds 也是可以的。如下 + +```php + [ + 'cluster' => [ + 'enable' => true, + 'name' => null, + 'seeds' => [ + '192.168.1.110:6379', + '192.168.1.111:6379', + ], + ], + ], +]; +``` \ No newline at end of file From ff090f05b06bb099984c4e15cd58faa32e0a60c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Thu, 7 Nov 2019 09:25:57 +0800 Subject: [PATCH 5/7] Optimized code. --- doc/zh/redis.md | 22 +++++++++++----------- src/redis/publish/redis.php | 2 +- src/redis/src/RedisConnection.php | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/doc/zh/redis.md b/doc/zh/redis.md index 94869ca5d..b67dd294f 100644 --- a/doc/zh/redis.md +++ b/doc/zh/redis.md @@ -8,15 +8,15 @@ composer require hyperf/redis ## 配置 -| 配置项 | 类型 | 默认值 | 备注 | -|:--------------:|:-------:|:-----------:|:--------------------------------:| -| host | string | 'localhost' | Redis地址 | -| auth | string | 无 | 密码 | -| port | integer | 6379 | 端口 | -| db | integer | 0 | DB | -| cluster.enable | boolean | false | 是否集群模式 | -| cluster.name | string | null | 集群名 | -| cluster.seeds | array | [] | 集群连接地址阿数组 ['host:port'] | +| 配置项 | 类型 | 默认值 | 备注 | +|:--------------:|:-------:|:-----------:|:------------------------------:| +| host | string | 'localhost' | Redis地址 | +| auth | string | 无 | 密码 | +| port | integer | 6379 | 端口 | +| db | integer | 0 | DB | +| cluster.enable | boolean | false | 是否集群模式 | +| cluster.name | string | null | 集群名 | +| cluster.seeds | array | [] | 集群连接地址数组 ['host:port'] | ```php (int) env('REDIS_PORT', 6379), 'db' => (int) env('REDIS_DB', 0), 'cluster' => [ - 'enable' => (bool) env('REDIS_ENABLE_CLUSTER', false), + 'enable' => (bool) env('REDIS_CLUSTER_ENABLE', false), 'name' => null, 'seeds' => [], ], @@ -72,7 +72,7 @@ return [ 'port' => (int) env('REDIS_PORT', 6379), 'db' => (int) env('REDIS_DB', 0), 'cluster' => [ - 'enable' => (bool) env('REDIS_ENABLE_CLUSTER', false), + 'enable' => (bool) env('REDIS_CLUSTER_ENABLE', false), 'name' => null, 'seeds' => [], ], diff --git a/src/redis/publish/redis.php b/src/redis/publish/redis.php index c2cdd9bec..13b99c87d 100644 --- a/src/redis/publish/redis.php +++ b/src/redis/publish/redis.php @@ -20,7 +20,7 @@ return [ 'reserved' => null, 'retry_interval' => 0, 'cluster' => [ - 'enable' => (bool) env('REDIS_ENABLE_CLUSTER', false), + 'enable' => (bool) env('REDIS_CLUSTER_ENABLE', false), 'name' => null, 'seeds' => [], ], diff --git a/src/redis/src/RedisConnection.php b/src/redis/src/RedisConnection.php index 0cbbad07f..c49f527c3 100644 --- a/src/redis/src/RedisConnection.php +++ b/src/redis/src/RedisConnection.php @@ -19,7 +19,7 @@ use Hyperf\Pool\Pool; use Psr\Container\ContainerInterface; /** - * @method select(int $db) + * @method bool select(int $db) */ class RedisConnection extends BaseConnection implements ConnectionInterface { From 9320c967e55072ed08ecbf3fb2e217886f540868 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Thu, 7 Nov 2019 09:33:38 +0800 Subject: [PATCH 6/7] Optimized code. --- .../Exception/InvalidRedisConnectionException.php | 15 ++++++++++++--- src/redis/src/Redis.php | 6 +++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/redis/src/Exception/InvalidRedisConnectionException.php b/src/redis/src/Exception/InvalidRedisConnectionException.php index efb3346ce..0754cfae0 100644 --- a/src/redis/src/Exception/InvalidRedisConnectionException.php +++ b/src/redis/src/Exception/InvalidRedisConnectionException.php @@ -1,10 +1,19 @@ factory->getPool($this->poolName); - return $pool->get(); + $connection = $pool->get(); + } + if (! $connection instanceof RedisConnection) { + throw new InvalidRedisConnectionException('The connection is not a valid RedisConnection.'); } return $connection; } From 00dd9c5596d7a6843eb4a6756e35d50939040b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Thu, 7 Nov 2019 11:13:42 +0800 Subject: [PATCH 7/7] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d45ee9ff0..0816cfb2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - [#812](https://github.com/hyperf/hyperf/pull/812) Added singleton crontab task support. - [#832](https://github.com/hyperf/hyperf/pull/832) Added `Hyperf\Utils\Codec\Json`. - [#833](https://github.com/hyperf/hyperf/pull/833) Added `Hyperf\Utils\Backoff`. -- [#852](https://github.com/hyperf/hyperf/pull/852) Added a `clear()` method for `Hyperf\Utils\Parallel` to clear adde callbacks. +- [#852](https://github.com/hyperf/hyperf/pull/852) Added a `clear()` method for `Hyperf\Utils\Parallel` to clear added callbacks. - [#873](https://github.com/hyperf/hyperf/pull/873) Added redis cluster. ## Fixed