Merge pull request #846 from yl/patch-1

Added *scan functions to Redis component.
This commit is contained in:
李铭昕 2019-11-07 12:35:24 +08:00 committed by GitHub
commit 6cb398e0b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 4 deletions

View File

@ -17,6 +17,7 @@
- [#835](https://github.com/hyperf/hyperf/pull/835) Fixed `Request::inputs` default value does not works.
- [#841](https://github.com/hyperf/hyperf/pull/841) Fixed migration does not take effect under multiple data sources.
- [#844](https://github.com/hyperf/hyperf/pull/844) Fixed the reader of `composer.json` does not support the root namespace.
- [#846](https://github.com/hyperf/hyperf/pull/846) Fixed `scan` `hScan` `zScan` and `sScan` don't works for Redis.
- [#850](https://github.com/hyperf/hyperf/pull/850) Fixed logger group does not works when the name is same.
## Optimized

View File

@ -18,6 +18,8 @@ use Hyperf\Utils\Context;
class Redis
{
use ScanCaller;
/**
* @var PoolFactory
*/

View File

@ -24,6 +24,8 @@ use Psr\Container\ContainerInterface;
*/
class RedisConnection extends BaseConnection implements ConnectionInterface
{
use ScanCaller;
/**
* @var \Redis
*/

View File

@ -0,0 +1,36 @@
<?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/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Redis;
trait ScanCaller
{
public function scan(&$cursor, $pattern = null, $count = 0)
{
return $this->__call('scan', [&$cursor, $pattern, $count]);
}
public function hScan($key, &$cursor, $pattern = null, $count = 0)
{
return $this->__call('hScan', [$key, &$cursor, $pattern, $count]);
}
public function zScan($key, &$cursor, $pattern = null, $count = 0)
{
return $this->__call('zScan', [$key, &$cursor, $pattern, $count]);
}
public function sScan($key, &$cursor, $pattern = null, $count = 0)
{
return $this->__call('sScan', [$key, &$cursor, $pattern, $count]);
}
}

View File

@ -34,11 +34,10 @@ class RedisProxyTest extends TestCase
{
protected function tearDown()
{
Mockery::close();
$redis = $this->getRedis();
$redis->del('test');
$redis->del('test:test');
$redis->flushDB();
Mockery::close();
}
public function testRedisOptionPrefix()
@ -65,6 +64,46 @@ class RedisProxyTest extends TestCase
$this->assertSame('s:3:"yyy";', $this->getRedis()->get('test'));
}
public function testRedisScan()
{
$redis = $this->getRedis();
$origin = ['scan:1', 'scan:2', 'scan:3', 'scan:4'];
foreach ($origin as $value) {
$redis->set($value, '1');
}
$it = null;
$result = [];
while (false !== $res = $redis->scan($it, 'scan:*', 2)) {
$result = array_merge($result, $res);
}
sort($result);
$this->assertEquals($origin, $result);
$this->assertSame(0, $it);
}
public function testRedisHScan()
{
$redis = $this->getRedis();
$origin = ['scan:1', 'scan:2', 'scan:3', 'scan:4'];
foreach ($origin as $value) {
$redis->hSet('scaner', $value, '1');
}
$it = null;
$result = [];
while (false !== $res = $redis->hScan('scaner', $it, 'scan:*', 2)) {
$result = array_merge($result, array_keys($res));
}
sort($result);
$this->assertEquals($origin, $result);
$this->assertSame(0, $it);
}
/**
* @param mixed $optinos
* @return \Redis