Skip to content

Commit 9a73506

Browse files
authored
Merge pull request #17 from MacFJA/abstraction
Remove hard dependency on Predis + allow other Redis lib
2 parents 79b7281 + 81c1d7f commit 9a73506

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3255
-443
lines changed

.unused.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
'enlightn/security-checker', // QA tool
2929
'php-parallel-lint/php-parallel-lint', // QA tool
3030
'sebastian/phpcpd', // QA tool
31+
'ukko/phpredis-phpdoc' // Stubs
3132
],
3233
'excludeDirectories' => [],
3334
'scanFiles' => [],

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: analyze fix-code test coverage mutation-test validation
1+
.PHONY: analyze fix-code test coverage validation integration-test
22

33
analyze: | vendor
44
$(COMPOSER) exec -v parallel-lint -- src
@@ -22,7 +22,10 @@ coverage: | vendor
2222
@if [ -z "`php -v | grep -i 'xdebug'`" ]; then echo "You need to install Xdebug in order to do this action"; exit 1; fi
2323
$(COMPOSER) exec -v phpunit -- --coverage-text --color
2424

25-
validation: fix-code analyze test coverage
25+
integration-test: | vendor
26+
$(COMPOSER) exec -v phpunit -- --group integration
27+
28+
validation: fix-code analyze test coverage integration-test
2629

2730
vendor: composer.json
2831
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist

README.md

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,62 @@ composer require macfja/redisearch
1212

1313
## Usage
1414

15+
### Get a Redis client
16+
17+
This lib can use several connector for Redis:
18+
- [Predis](https://github.com/predis/predis/wiki) - Pure PHP implementation
19+
- [Phpredis](https://github.com/phpredis/phpredis) - PHP extension
20+
- [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
27+
28+
You can pick the connector depending of your need.
29+
30+
```php
31+
$clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();
32+
33+
// With Predis
34+
$client = $clientFacade->getClient(new \Predis\Client(/* ... */));
35+
36+
// With Phpredis extension
37+
$client = $clientFacade->getClient(new \Redis([/* ... */]));
38+
39+
// With Phpiredis extension
40+
$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(/* ... */));
59+
```
60+
61+
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:
62+
```php
63+
$clientFacade = new \MacFJA\RediSearch\Redis\Client\ClientFacade();
64+
$clientFacade->addFactory(\MyVendor\MyPackage\MyRedisClient::class);
65+
```
66+
1567
### Create a new index
1668

1769
```php
18-
$client = new \Predis\Client(/* ... */);
70+
$client = /* ... */;
1971
$builder = new \MacFJA\RediSearch\IndexBuilder();
2072

2173
// Field can be create in advance
@@ -38,9 +90,9 @@ This will give you a new instance of the builder with the configured data.
3890
### Add a document
3991

4092
```php
41-
$client = new \Predis\Client(/* ... */);
93+
$client = /* ... */;
4294
$index = new \MacFJA\RediSearch\Index('person', $client);
43-
$index->addFromArray([
95+
$index->addDocumentFromArray([
4496
'firstname' => 'Joe',
4597
'lastname' => 'Doe',
4698
'age' => 30,
@@ -51,15 +103,15 @@ $index->addFromArray([
51103
### Search
52104

53105
```php
54-
$client = new \Predis\Client(/* ... */);
106+
$client = /* ... */;
55107
$search = new \MacFJA\RediSearch\Redis\Command\Search();
56108

57109
$search
58110
->setIndex('person')
59111
->setQuery('Doe')
60112
->setHighlight(['lastname'])
61113
->setWithScores();
62-
$results = $client->executeCommand($search);
114+
$results = $client->execute($search);
63115
```
64116

65117
#### Create a search query
@@ -96,9 +148,8 @@ use MacFJA\RediSearch\Redis\Command\AggregateCommand\GroupByOption;
96148
use MacFJA\RediSearch\Redis\Command\AggregateCommand\ReduceOption;
97149
use MacFJA\RediSearch\Redis\Command\Search;
98150
use MacFJA\RediSearch\Redis\Command\SugGet;
99-
use Predis\Client;
100151

101-
$client = new Client(/* ... */);
152+
$client = /* ... */;
102153

103154
$query = '@age:[(17 +inf] %john%';
104155
$search = new Search();
@@ -123,13 +174,7 @@ $suggestion->setDictionary('names')
123174
->setPrefix('john')
124175
->setFuzzy();
125176

126-
$result = $client->pipeline()
127-
->executeCommand($search)
128-
->executeCommand($stats)
129-
->executeCommand($aggregate)
130-
->executeCommand($suggestion)
131-
->execute()
132-
;
177+
$result = $client->pipeline($search, $stats, $aggregate, $suggestion);
133178

134179
// $result[0] is the search result
135180
// $result[1] is the first aggregation result

composer.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,41 @@
1919
"php": "^7.2",
2020
"ext-intl": "*",
2121
"composer/semver": "^3.2",
22-
"predis/predis": "^1.1",
2322
"respect/validation": "^2.0"
2423
},
2524
"require-dev": {
25+
"amphp/redis": "^1.0",
26+
"cheprasov/php-redis-client": "^1.10",
27+
"colinmollenhour/credis": "^1.12",
2628
"enlightn/security-checker": "^1.9",
2729
"ergebnis/composer-normalize": "^2.13",
2830
"friendsofphp/php-cs-fixer": "^3.0",
31+
"geometria-lab/rediska": "^0.5.10",
2932
"insolita/unused-scanner": "^2.3",
3033
"php-parallel-lint/php-parallel-lint": "^1.3",
3134
"phpmd/phpmd": "^2.10",
3235
"phpstan/phpstan": "^0.12.92",
3336
"phpunit/phpunit": "^8.5",
37+
"predis/predis": "^1.1",
38+
"ptrofimov/tinyredisclient": "^1.1",
39+
"redisent/redisent": "dev-master",
3440
"roave/security-advisories": "dev-latest",
3541
"rskuipers/php-assumptions": "^0.8.0",
3642
"sebastian/phpcpd": "^4.1",
43+
"ukko/phpredis-phpdoc": "dev-master",
3744
"vimeo/psalm": "^4.7"
3845
},
46+
"suggest": {
47+
"ext-phpiredis": "To use Phpiredis extension implementation",
48+
"ext-redis": "To use Phpredis extension implementation",
49+
"amphp/redis": "To use AmpPhp implementation",
50+
"cheprasov/php-redis-client": "To use Cheprasov implementation",
51+
"colinmollenhour/credis": "To use Credis implementation",
52+
"geometria-lab/rediska": "To use Rediska implementation",
53+
"predis/predis": "To use Predis implementation",
54+
"ptrofimov/tinyredisclient": "To use TinyRedisClient implementation",
55+
"redisent/redisent": "To use Redisent implementation"
56+
},
3957
"config": {
4058
"platform": {
4159
"php": "7.2"

0 commit comments

Comments
 (0)