Skip to content

Commit 7daa2f6

Browse files
committed
Add client for ReactPHP Redis
1 parent 837bf52 commit 7daa2f6

15 files changed

+146
-66
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010

11+
- Add client for ReactPHP Redis
1112
- Path validation for JSON fields
1213
- Helper function get current RedisJSON version + (abstraction)
1314
- (dev) More unit tests

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This lib can use several connector for Redis:
2424
- [Rediska](https://github.com/Shumkov/Rediska) - Pure PHP implementation
2525
- [Redisent](https://github.com/jdp/redisent) - Pure PHP implementation
2626
- [TinyRedis](https://github.com/ptrofimov/tinyredisclient) - Pure PHP implementation
27+
- [RecatPHP Redis](https://github.com/clue/reactphp-redis) - Pure PHP Async implementation
2728

2829
You can pick the connector depending on your need.
2930

@@ -56,6 +57,9 @@ $client = $clientFacade->getClient(new \TinyRedisClient(/* ... */));
5657

5758
// With Credis
5859
$client = $clientFacade->getClient(new \Credis_Client(/* ... */));
60+
61+
// With ReactPHP Redis
62+
$client = $clientFacade->getClient(new (new \Clue\React\Redis\Factory())->createClient(/* ... */));
5963
```
6064

6165
You can add your own implementation, all you need is to implement the interface `\MacFJA\RediSearch\Redis\Client` and add it to the client facace with:

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"ext-mbstring": "*",
3131
"amphp/redis": "^1.0",
3232
"cheprasov/php-redis-client": "^1.10",
33+
"clue/redis-react": "^2.6",
3334
"colinmollenhour/credis": "^1.12",
3435
"enlightn/security-checker": "^1.9",
3536
"ergebnis/composer-normalize": "^2.13",
@@ -42,6 +43,7 @@
4243
"phpunit/phpunit": "^8.5 || ^9.3",
4344
"predis/predis": "^1.1 || ^2.0",
4445
"ptrofimov/tinyredisclient": "^1.1",
46+
"react/async": "^3.0",
4547
"redisent/redisent": "dev-master",
4648
"roave/security-advisories": "dev-latest",
4749
"rskuipers/php-assumptions": "^0.8.0",

src/Redis/Client/AbstractClient.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public function pipeline(Command ...$commands): array
4141
}, $results, array_keys($results));
4242
}
4343

44+
public function execute(Command $command)
45+
{
46+
$args = [$command->getId()];
47+
$commandArgs = $command->getArguments();
48+
if (count($commandArgs) > 0) {
49+
array_push($args, ...$commandArgs);
50+
}
51+
$result = $this->executeRaw(...$args);
52+
53+
return $command->parseResponse($result);
54+
}
55+
4456
/**
4557
* @return array<mixed>
4658
*/

src/Redis/Client/AmpRedisClient.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@ public static function make($redis): Client
5353
return new self($redis);
5454
}
5555

56-
public function execute(Command $command)
57-
{
58-
/** @var Promise<mixed> $query */
59-
$query = $this->redis->query($command->getId(), ...array_map('strval', $command->getArguments()));
60-
$result = wait($query);
61-
62-
return $command->parseResponse($result);
63-
}
64-
6556
public function executeRaw(...$args)
6657
{
6758
/** @var Promise<mixed> $query */
@@ -77,6 +68,9 @@ public static function supports($redis): bool
7768
&& function_exists('\\Amp\\Promise\\wait');
7869
}
7970

71+
/**
72+
* @codeCoverageIgnore
73+
*/
8074
public function pipeline(Command ...$commands): array
8175
{
8276
false === static::$disableNotice

src/Redis/Client/CheprasovRedisClient.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ public static function make($redis): Client
5252
return new self($redis);
5353
}
5454

55-
public function execute(Command $command)
56-
{
57-
$response = $this->redis->executeRaw(array_merge([$command->getId()], $command->getArguments()));
58-
59-
return $command->parseResponse($response);
60-
}
61-
6255
public function executeRaw(...$args)
6356
{
6457
return $this->redis->executeRaw(array_map('strval', $args));

src/Redis/Client/ClientFacade.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
namespace MacFJA\RediSearch\Redis\Client;
2323

2424
use Amp\Redis\Redis as AmpRedis;
25+
use Clue\React\Redis\Client as ReactClient;
2526
use Credis_Client as CredisRedis;
2627
use MacFJA\RediSearch\Redis\Client;
2728
use Predis\ClientInterface as PredisRedis;
29+
use React\Promise\Promise as ReactPromise;
2830
use Redis as PhpredisRedis;
2931
use RedisClient\Client\AbstractRedisClient as CheprasovRedis;
3032
use redisent\Redis as RedisentRedis;
@@ -44,10 +46,11 @@ class ClientFacade
4446
RediskaClient::class,
4547
AmpRedisClient::class,
4648
TinyRedisClient::class,
49+
ReactRedisClient::class,
4750
];
4851

4952
/**
50-
* @param AmpRedis|CheprasovRedis|CredisRedis|mixed|PhpredisRedis|PredisRedis|RedisentRedis|RediskaRedis|resource $redis
53+
* @param AmpRedis|CheprasovRedis|CredisRedis|mixed|PhpredisRedis|PredisRedis|ReactClient|ReactPromise|RedisentRedis|RediskaRedis|resource $redis
5154
*/
5255
public function getClient($redis): Client
5356
{

src/Redis/Client/CredisClient.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ public static function make($redis): Client
5151
return new self($redis);
5252
}
5353

54-
public function execute(Command $command)
55-
{
56-
$result = $this->redis->__call($command->getId(), $command->getArguments());
57-
58-
return $command->parseResponse($this->fixFalseToNull($result));
59-
}
60-
6154
public function executeRaw(...$args)
6255
{
6356
$command = array_shift($args);

src/Redis/Client/PhpiredisClient.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ private function __construct($redis)
5151
$this->redis = $redis;
5252
}
5353

54-
public function execute(Command $command)
55-
{
56-
$rawResponse = phpiredis_command_bs($this->redis, array_merge([$command->getId()], $command->getArguments()));
57-
58-
return $command->parseResponse($rawResponse);
59-
}
60-
6154
public static function supports($redis): bool
6255
{
6356
if (!is_resource($redis)

src/Redis/Client/PredisClient.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ private function __construct(ClientInterface $redis)
5555
$this->redis = $redis;
5656
}
5757

58-
public function execute(Command $command)
59-
{
60-
$rawResponse = $this->redis->executeCommand(self::createRawCommand(array_merge([$command->getId()], $command->getArguments())));
61-
62-
return $command->parseResponse($rawResponse);
63-
}
64-
6558
public static function supports($redis): bool
6659
{
6760
if (

0 commit comments

Comments
 (0)