Skip to content

Commit

Permalink
Merge pull request #846 from yl/patch-1
Browse files Browse the repository at this point in the history
Added *scan functions to Redis component.
  • Loading branch information
limingxinleo committed Nov 7, 2019
2 parents 3da805e + 9227c5d commit 6cb398e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/redis/src/Redis.php
Expand Up @@ -18,6 +18,8 @@

class Redis
{
use ScanCaller;

/**
* @var PoolFactory
*/
Expand Down
2 changes: 2 additions & 0 deletions src/redis/src/RedisConnection.php
Expand Up @@ -24,6 +24,8 @@
*/
class RedisConnection extends BaseConnection implements ConnectionInterface
{
use ScanCaller;

/**
* @var \Redis
*/
Expand Down
36 changes: 36 additions & 0 deletions src/redis/src/ScanCaller.php
@@ -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]);
}
}
47 changes: 43 additions & 4 deletions src/redis/tests/RedisProxyTest.php
Expand Up @@ -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()
Expand All @@ -65,6 +64,46 @@ public function testRedisOptionSerializer()
$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
Expand Down

0 comments on commit 6cb398e

Please sign in to comment.