diff --git a/src/AbstractRedisConnection.php b/src/AbstractRedisConnection.php index 2706fbd..a9784f0 100644 --- a/src/AbstractRedisConnection.php +++ b/src/AbstractRedisConnection.php @@ -88,6 +88,10 @@ 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; } diff --git a/src/Operator/Hashes/HashGetAll.php b/src/Operator/Hashes/HashGetAll.php index c9abbfd..8de686a 100644 --- a/src/Operator/Hashes/HashGetAll.php +++ b/src/Operator/Hashes/HashGetAll.php @@ -21,11 +21,8 @@ public function getId() */ public function parseResponse($data) { - if ($data === false) { - return false; - } + $result = array(); - $result = []; for ($i = 0; $i < count($data); ++$i) { $result[$data[$i]] = $data[++$i]; } diff --git a/src/Operator/Hashes/HashGetMultiple.php b/src/Operator/Hashes/HashGetMultiple.php index b44c263..818933a 100644 --- a/src/Operator/Hashes/HashGetMultiple.php +++ b/src/Operator/Hashes/HashGetMultiple.php @@ -21,14 +21,10 @@ public function getId() */ public function parseResponse($data) { - if ($data === false) { - return false; - } - $result = []; $hashKeys = $this->getArgument(1); foreach ($data as $key => $value) { - if (!isset($hashKeys[$key])) { + if (! isset($hashKeys[$key])) { continue; } diff --git a/src/Operator/Keys/KeyPreciseExpire.php b/src/Operator/Keys/KeyPreciseExpire.php index fc0033b..0e9bc59 100644 --- a/src/Operator/Keys/KeyPreciseExpire.php +++ b/src/Operator/Keys/KeyPreciseExpire.php @@ -7,12 +7,12 @@ class KeyPreciseExpire extends Command { /** - * [Keys] pexpire + * [Keys] pexipre * * @return string */ public function getId() { - return 'pexpire'; + return 'pexipre'; } } diff --git a/src/Operator/Processor/PrefixProcessor.php b/src/Operator/Processor/PrefixProcessor.php index 6c0ea01..940cd12 100644 --- a/src/Operator/Processor/PrefixProcessor.php +++ b/src/Operator/Processor/PrefixProcessor.php @@ -95,7 +95,6 @@ public function init() 'ZADD' => 'static::first', 'ZINCRBY' => 'static::first', 'ZREM' => 'static::first', - 'ZDELETE' => 'static::first', 'ZRANGE' => 'static::first', 'ZREVRANGE' => 'static::first', 'ZRANGEBYSCORE' => 'static::first', diff --git a/src/Pool/RedisPool.php b/src/Pool/RedisPool.php index 96c2bf2..14bb74a 100644 --- a/src/Pool/RedisPool.php +++ b/src/Pool/RedisPool.php @@ -39,6 +39,9 @@ public function createConnection(): ConnectionInterface $redis = new SyncRedisConnection($this); } + $dbIndex = $this->poolConfig->getDb(); + $redis->select($dbIndex); + return $redis; } } diff --git a/src/RedisConnection.php b/src/RedisConnection.php index e088de3..a2a5aa4 100644 --- a/src/RedisConnection.php +++ b/src/RedisConnection.php @@ -33,10 +33,6 @@ public function createConnection() { $redis = $this->initRedis(); $this->connection = $redis; - - /** @var RedisPoolConfig $config */ - $config = $this->getPool()->getPoolConfig(); - $redis->select($config->getDb()); } /** diff --git a/src/SyncRedisConnection.php b/src/SyncRedisConnection.php index 90c5290..827db2c 100644 --- a/src/SyncRedisConnection.php +++ b/src/SyncRedisConnection.php @@ -39,10 +39,6 @@ public function createConnection() $redis->setOption(\Redis::OPT_PREFIX, $prefix); } $this->connection = $redis; - - /** @var RedisPoolConfig $config */ - $config = $this->getPool()->getPoolConfig(); - $redis->select($config->getDb()); } /** diff --git a/test/Cases/HashTest.php b/test/Cases/HashTest.php index 0f4d881..62254dd 100644 --- a/test/Cases/HashTest.php +++ b/test/Cases/HashTest.php @@ -26,18 +26,6 @@ public function testHmsetAndHmget() ]; $values = $this->redis->hMGet($key, ['NotExistKey', 'NotExistKey2']); $this->assertEquals($data, $values); - - $this->redis->set($key, 'xxxxx'); - $result = $this->redis->hMGet($key,['key']); - $this->assertFalse($result); - - $this->redis->delete($key); - $result = $this->redis->hMGet($key, ['key']); - $this->assertEquals(['key' => false], $result); - - $this->redis->sAdd($key, 'xxxxx'); - $result = $this->redis->hMGet($key, ['key']); - $this->assertFalse($result); } public function testHmsetAndHmgetByCo() @@ -55,18 +43,6 @@ public function testHGetAll() $result = $this->redis->hGetAll($key); $this->assertEquals(['key' => 'value', 'key2' => 'value2', 'key3' => 'value3'], $result); - - $this->redis->set($key, 'xxxxx'); - $result = $this->redis->hGetAll($key); - $this->assertFalse($result); - - $this->redis->delete($key); - $result = $this->redis->hGetAll($key); - $this->assertEquals([], $result); - - $this->redis->sAdd($key, 'xxxxx'); - $result = $this->redis->hGetAll($key); - $this->assertFalse($result); } public function testHGetAllByCo() diff --git a/test/Cases/PoolTest.php b/test/Cases/PoolTest.php index 5a83516..a72a8cb 100644 --- a/test/Cases/PoolTest.php +++ b/test/Cases/PoolTest.php @@ -4,7 +4,6 @@ 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; @@ -74,16 +73,4 @@ 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); - } } \ No newline at end of file