Skip to content

Commit

Permalink
Adds Redis::pipeline() and Redis::transaction() support (#5639)
Browse files Browse the repository at this point in the history
Co-authored-by: 李铭昕 <715557344@qq.com>
  • Loading branch information
huangdijia and limingxinleo committed Apr 14, 2023
1 parent 6377ea7 commit 90feb18
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Added

- [#5634](https://github.com/hyperf/hyperf/pull/5634) Added `Hyperf\Stringable\str()` helper function.
- [#5639](https://github.com/hyperf/hyperf/pull/5639) Added `Redis::pipeline()` and `Redis::transaction()` support.

## Optimized

Expand Down
1 change: 1 addition & 0 deletions src/redis/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ext-redis": "*",
"hyperf/contract": "~3.0.0",
"hyperf/pool": "~3.0.0",
"hyperf/tappable": "~3.0.0",
"hyperf/utils": "~3.0.0",
"psr/container": "^1.0|^2.0"
},
Expand Down
3 changes: 2 additions & 1 deletion src/redis/src/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/
class Redis
{
use ScanCaller;
use Traits\ScanCaller;
use Traits\MultiExec;

protected string $poolName = 'default';

Expand Down
3 changes: 2 additions & 1 deletion src/redis/src/RedisConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
*/
class RedisConnection extends BaseConnection implements ConnectionInterface
{
use ScanCaller;
use Traits\ScanCaller;
use Traits\MultiExec;

protected Redis|RedisCluster|null $connection = null;

Expand Down
23 changes: 4 additions & 19 deletions src/redis/src/ScanCaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,10 @@
*/
namespace Hyperf\Redis;

/**
* @deprecated since 3.1, use Hyperf\Redis\Traits\ScanCaller instead.
*/
trait ScanCaller
{
public function scan(&$cursor, ...$arguments)
{
return $this->__call('scan', array_merge([&$cursor], $arguments));
}

public function hScan($key, &$cursor, ...$arguments)
{
return $this->__call('hScan', array_merge([$key, &$cursor], $arguments));
}

public function zScan($key, &$cursor, ...$arguments)
{
return $this->__call('zScan', array_merge([$key, &$cursor], $arguments));
}

public function sScan($key, &$cursor, ...$arguments)
{
return $this->__call('sScan', array_merge([$key, &$cursor], $arguments));
}
use Traits\ScanCaller;
}
44 changes: 44 additions & 0 deletions src/redis/src/Traits/MultiExec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Redis\Traits;

use Redis;
use RedisCluster;

use function Hyperf\Tappable\tap;

trait MultiExec
{
/**
* Execute commands in a pipeline.
*
* @return array|Redis
*/
public function pipeline(callable $callback = null)
{
$pipeline = $this->__call('pipeline', []);

return is_null($callback) ? $pipeline : tap($pipeline, $callback)->exec();
}

/**
* Execute commands in a transaction.
*
* @return array|Redis|RedisCluster
*/
public function transaction(callable $callback = null)
{
$transaction = $this->__call('multi', []);

return is_null($callback) ? $transaction : tap($transaction, $callback)->exec();
}
}
35 changes: 35 additions & 0 deletions src/redis/src/Traits/ScanCaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Redis\Traits;

trait ScanCaller
{
public function scan(&$cursor, ...$arguments)
{
return $this->__call('scan', array_merge([&$cursor], $arguments));
}

public function hScan($key, &$cursor, ...$arguments)
{
return $this->__call('hScan', array_merge([$key, &$cursor], $arguments));
}

public function zScan($key, &$cursor, ...$arguments)
{
return $this->__call('zScan', array_merge([$key, &$cursor], $arguments));
}

public function sScan($key, &$cursor, ...$arguments)
{
return $this->__call('sScan', array_merge([$key, &$cursor], $arguments));
}
}
39 changes: 38 additions & 1 deletion src/redis/tests/RedisProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Hyperf\Redis\Redis;
use Mockery;
use PHPUnit\Framework\TestCase;
use RedisCluster;

use function Hyperf\Coroutine\go;

Expand Down Expand Up @@ -121,6 +122,42 @@ public function testRedisHScan()
$this->assertSame(0, $it);
}

public function testPipeline()
{
$pipe = $this->getRedis()->pipeline();
$this->assertInstanceOf(\Redis::class, $pipe);

$key = 'pipeline:' . uniqid();

$this->getRedis()->pipeline(function (\Redis $pipe) use ($key) {
$pipe->incr($key);
$pipe->incr($key);
$pipe->incr($key);
});

$this->assertEquals(3, $this->getRedis()->get($key));

$this->getRedis()->del($key);
}

public function testTransaction()
{
$transaction = $this->getRedis()->transaction();
$this->assertInstanceOf(\Redis::class, $transaction);

$key = 'transaction:' . uniqid();

$this->getRedis()->transaction(function (\Redis|RedisCluster $transaction) use ($key) {
$transaction->incr($key);
$transaction->incr($key);
$transaction->incr($key);
});

$this->assertEquals(3, $this->getRedis()->get($key));

$this->getRedis()->del($key);
}

public function testRedisPipeline()
{
$redis = $this->getRedis();
Expand Down Expand Up @@ -157,7 +194,7 @@ public function testRedisPipeline()

/**
* @param mixed $optinos
* @return \Redis
* @return \Redis|Redis
*/
private function getRedis($optinos = [])
{
Expand Down
1 change: 1 addition & 0 deletions src/redis/tests/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

use function Hyperf\Coroutine\defer;
use function Hyperf\Coroutine\go;
use function Hyperf\Coroutine\parallel;

/**
* @internal
Expand Down

0 comments on commit 90feb18

Please sign in to comment.