Skip to content

Commit e108895

Browse files
committed
Add TinyRedis + update Readme
1 parent 764fd38 commit e108895

File tree

10 files changed

+207
-29
lines changed

10 files changed

+207
-29
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ This lib can use several connector for Redis:
1818
- [Predis](https://github.com/predis/predis/wiki) - Pure PHP implementation
1919
- [Phpredis](https://github.com/phpredis/phpredis) - PHP extension
2020
- [Phpiredis](https://github.com/nrk/phpiredis) - PHP extension depending on [hiredis](https://github.com/redis/hiredis)
21+
- [Amp\Redis](https://github.com/amphp/redis) - Pure PHP Async implementation
22+
- [cheprasov/php-redis-client](https://github.com/cheprasov/php-redis-client) - Pure PHP implementation
23+
- [Credis](https://github.com/colinmollenhour/credis) - Pure PHP implementation
24+
- [Rediska](https://github.com/Shumkov/Rediska) - Pure PHP implementation
25+
- [Redisent](https://github.com/jdp/redisent) - Pure PHP implementation
26+
- [TinyRedis](https://github.com/ptrofimov/tinyredisclient) - Pure PHP implementation
2127

2228
You can pick the connector depending of your need.
2329

@@ -28,10 +34,28 @@ $clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();
2834
$client = $clientFacade->getClient(new \Predis\Client(/* ... */));
2935

3036
// With Phpredis extension
31-
$client = $clientFacade->getClient(new Redis([/* ... */]));
37+
$client = $clientFacade->getClient(new \Redis([/* ... */]));
3238

3339
// With Phpiredis extension
3440
$client = $clientFacade->getClient(phpiredis_connect($host));
41+
42+
// With Amp\Redis
43+
$client = $clientFacade->getClient(new \Amp\Redis\Redis(new RemoteExecutor(Config::fromUri(/* ... */))));
44+
45+
// With Cheprasov
46+
$client = $clientFacade->getClient(new \RedisClient\Client\Version\RedisClient6x0([/* ... */]));
47+
48+
// With Rediska
49+
$client = $clientFacade->getClient(new \Rediska(['servers' => [[/* ... */]]]));
50+
51+
// With Redisent
52+
$client = $clientFacade->getClient(new \redisent\Redis(/* ... */));
53+
54+
// With TinyRedisClient
55+
$client = $clientFacade->getClient(new \TinyRedisClient(/* ... */));
56+
57+
// With Credis
58+
$client = $clientFacade->getClient(new \Credis_Client(/* ... */));
3559
```
3660

3761
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"phpstan/phpstan": "^0.12.92",
3636
"phpunit/phpunit": "^8.5",
3737
"predis/predis": "^1.1",
38+
"ptrofimov/tinyredisclient": "^1.1",
3839
"redisent/redisent": "dev-master",
3940
"roave/security-advisories": "dev-latest",
4041
"rskuipers/php-assumptions": "^0.8.0",
@@ -44,12 +45,13 @@
4445
},
4546
"suggest": {
4647
"ext-phpiredis": "To use Phpiredis extension implementation",
47-
"ext-phpredis": "To use Phpredis extension implementation",
48+
"ext-redis": "To use Phpredis extension implementation",
4849
"amphp/redis": "To use AmpPhp implementation",
4950
"cheprasov/php-redis-client": "To use Cheprasov implementation",
5051
"colinmollenhour/credis": "To use Credis implementation",
5152
"geometria-lab/rediska": "To use Rediska implementation",
5253
"predis/predis": "To use Predis implementation",
54+
"ptrofimov/tinyredisclient": "To use TinyRedisClient implementation",
5355
"redisent/redisent": "To use Redisent implementation"
5456
},
5557
"config": {

composer.lock

Lines changed: 44 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Redis/Client/AbstractClient.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
abstract class AbstractClient implements Client
2929
{
30+
/** @var bool */
31+
public static $disableNotice = false;
32+
3033
public function pipeline(Command ...$commands): array
3134
{
3235
$results = $this->doPipeline(...$commands);
@@ -68,28 +71,29 @@ protected function getMissingMessage(string $name, bool $isExtension, array $cla
6871
if (true === $isExtension) {
6972
$message = 'The extension '.$name.' is missing.'.PHP_EOL.'Install the extension';
7073
}
71-
$message .= ' or use a polyfill that provide';
74+
$message .= ' or use a polyfill that provide ';
7275

7376
$classesMessage = null;
7477
if (1 === count($classes)) {
75-
$classesMessage = ' the class "'.reset($classes).'"';
78+
$classesMessage = 'the class "'.reset($classes).'"';
7679
} elseif (count($classes) > 1) {
77-
$classesMessage = ' the classes "'.implode('", "', $classes).'"';
80+
$classesMessage = 'the classes "'.implode('", "', $classes).'"';
7881
}
7982
$methodsMessage = null;
8083
if (1 === count($methods)) {
81-
$methodsMessage = ' the method "'.reset($methods).'"';
84+
$methodsMessage = 'the method "'.reset($methods).'"';
8285
} elseif (count($methods) > 1) {
83-
$methodsMessage = ' the methods "'.implode('", "', $methods).'"';
86+
$methodsMessage = 'the methods "'.implode('", "', $methods).'"';
8487
}
8588
$functionsMessage = null;
8689
if (1 === count($functions)) {
87-
$functionsMessage = ' the function "'.reset($functions).'"';
90+
$functionsMessage = 'the function "'.reset($functions).'"';
8891
} elseif (count($functions) > 1) {
89-
$functionsMessage = ' the functions "'.implode('", "', $functions).'"';
92+
$functionsMessage = 'the functions "'.implode('", "', $functions).'"';
9093
}
9194

9295
$message .= implode(' and ', array_filter([$classesMessage, $methodsMessage, $functionsMessage]));
96+
$message .= '.';
9397

9498
return $message;
9599
}

src/Redis/Client/AmpRedisClient.php

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

55-
/*[]
56-
* @inheritDoc
57-
*/
5855
public function execute(Command $command)
5956
{
6057
$result = wait($this->redis->query($command->getId(), ...array_map('strval', $command->getArguments())));
@@ -77,7 +74,8 @@ public static function supports($redis): bool
7774

7875
protected function doPipeline(Command ...$commands): array
7976
{
80-
trigger_error('Warning, \\Amp\\Redis\\Redis don\'t use a real Redis Pipeline', E_USER_NOTICE);
77+
false === static::$disableNotice
78+
&& trigger_error('Warning, \\Amp\\Redis\\Redis don\'t use a real Redis Pipeline', E_USER_NOTICE);
8179

8280
return wait(all(
8381
array_map(function (Command $command) {

src/Redis/Client/CheprasovRedisClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public static function supports($redis): bool
7474

7575
protected function doPipeline(Command ...$commands): array
7676
{
77-
trigger_error('Warning, a workaround is used to enable custom command in pipeline for \\RedisClient\\Pipeline\\PipelineInterface', E_USER_NOTICE);
77+
false === static::$disableNotice
78+
&& trigger_error('Warning, a workaround is used to enable custom command in pipeline for \\RedisClient\\Pipeline\\PipelineInterface', E_USER_NOTICE);
7879

7980
return $this->redis->pipeline(function (PipelineInterface $pipeline) use ($commands): void {
8081
foreach ($commands as $command) {

src/Redis/Client/CredisClient.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace MacFJA\RediSearch\Redis\Client;
2323

2424
use Credis_Client;
25+
use function is_array;
2526
use MacFJA\RediSearch\Redis\Client;
2627
use MacFJA\RediSearch\Redis\Command;
2728
use RuntimeException;
@@ -38,7 +39,7 @@ private function __construct(Credis_Client $redis)
3839
{
3940
if (!self::supports($redis)) {
4041
throw new RuntimeException($this->getMissingMessage('Credis', false, [
41-
Credis_Client::class => ['__call', 'pipeline', 'exec'],
42+
Credis_Client::class => ['__call'],
4243
]));
4344
}
4445
$this->redis = $redis;
@@ -53,22 +54,20 @@ public function execute(Command $command)
5354
{
5455
$result = $this->redis->__call($command->getId(), $command->getArguments());
5556

56-
return $command->parseResponse($result);
57+
return $command->parseResponse($this->fixFalseToNull($result));
5758
}
5859

5960
public function executeRaw(...$args)
6061
{
6162
$command = array_shift($args);
6263

63-
return $this->redis->__call($command, $args);
64+
return $this->fixFalseToNull($this->redis->__call($command, $args));
6465
}
6566

6667
public static function supports($redis): bool
6768
{
6869
return $redis instanceof Credis_Client
69-
&& method_exists($redis, '__call')
70-
&& method_exists($redis, 'pipeline')
71-
&& method_exists($redis, 'exec');
70+
&& method_exists($redis, '__call');
7271
}
7372

7473
protected function doPipeline(Command ...$commands): array
@@ -78,6 +77,25 @@ protected function doPipeline(Command ...$commands): array
7877
$pipeline = $pipeline->__call($command->getId(), $command->getArguments());
7978
}
8079

81-
return $pipeline->exec();
80+
return $this->fixFalseToNull($pipeline->exec());
81+
}
82+
83+
/**
84+
* @param false|mixed $response
85+
*
86+
* @return null|mixed
87+
*/
88+
private function fixFalseToNull($response)
89+
{
90+
if (false === $response) {
91+
return null;
92+
}
93+
if (is_array($response)) {
94+
foreach ($response as &$item) {
95+
$item = $this->fixFalseToNull($item);
96+
}
97+
}
98+
99+
return $response;
82100
}
83101
}

src/Redis/Client/TinyRedisClient.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright MacFJA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
9+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
10+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
14+
* Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
namespace MacFJA\RediSearch\Redis\Client;
23+
24+
use MacFJA\RediSearch\Redis\Client;
25+
use MacFJA\RediSearch\Redis\Command;
26+
use RuntimeException;
27+
28+
/**
29+
* @codeCoverageIgnore
30+
*/
31+
class TinyRedisClient extends AbstractClient
32+
{
33+
/** @var \TinyRedisClient */
34+
private $redis;
35+
36+
public function __construct(\TinyRedisClient $redis)
37+
{
38+
if (!self::supports($redis)) {
39+
throw new RuntimeException($this->getMissingMessage(
40+
'TinyRedis',
41+
false,
42+
[\TinyRedisClient::class => ['__call']]
43+
));
44+
}
45+
$this->redis = $redis;
46+
}
47+
48+
public function pipeline(Command ...$commands): array
49+
{
50+
false === static::$disableNotice
51+
&& trigger_error('Warning, \\TinyRedisClient don\'t use a real Redis Pipeline', E_USER_NOTICE);
52+
53+
return array_map(function (Command $command) {
54+
return $this->execute($command);
55+
}, $commands);
56+
}
57+
58+
public function execute(Command $command)
59+
{
60+
$result = $this->redis->__call($command->getId(), $command->getArguments());
61+
62+
return $command->parseResponse($result);
63+
}
64+
65+
public function executeRaw(...$args)
66+
{
67+
$command = array_shift($args);
68+
69+
return $this->redis->__call($command, $args);
70+
}
71+
72+
public static function supports($redis): bool
73+
{
74+
return $redis instanceof \TinyRedisClient
75+
&& method_exists($redis, '__call');
76+
}
77+
78+
public static function make($redis): Client
79+
{
80+
return new self($redis);
81+
}
82+
83+
protected function doPipeline(Command ...$commands): array
84+
{
85+
return [];
86+
}
87+
}

src/Redis/Initializer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace MacFJA\RediSearch\Redis;
2323

2424
use function count;
25+
use MacFJA\RediSearch\Redis\Client\AbstractClient;
2526
use MacFJA\RediSearch\Redis\Command\Aggregate;
2627
use MacFJA\RediSearch\Redis\Command\AliasAdd;
2728
use MacFJA\RediSearch\Redis\Command\AliasDel;
@@ -64,7 +65,7 @@ public function create(): Rediska_Connection_Exec
6465
{
6566
$connections = $this->_rediska->getConnections();
6667

67-
if (count($connections) > 1) {
68+
if (false === AbstractClient::$disableNotice && count($connections) > 1) {
6869
trigger_error('Warning, Multiple redis connections exists, only the first connection will be used', E_USER_NOTICE);
6970
}
7071
$connection = reset($connections);

0 commit comments

Comments
 (0)