Skip to content

Commit 84dd032

Browse files
authored
Fix #7: Add cluster support and raise the predis/predis version to ^2.1
1 parent 3961a3a commit 84dd032

25 files changed

Lines changed: 1012 additions & 122 deletions

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
/psalm.xml export-ignore
3535
/tests export-ignore
3636
/docs export-ignore
37+
/Makefile export-ignore
38+
/rector.php export-ignore
3739

3840
# Avoid merge conflicts in CHANGELOG
3941
# https://about.gitlab.com/2015/02/10/gitlab-reduced-merge-conflicts-by-90-percent-with-changelog-placeholders/

.github/workflows/build.yml

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
- 'psalm.xml'
1111

1212
push:
13+
branches: [ 'master' ]
1314
paths-ignore:
1415
- 'docs/**'
1516
- 'README.md'
@@ -19,64 +20,22 @@ on:
1920
- 'infection.json.dist'
2021
- 'psalm.xml'
2122

22-
name: build
23+
name: Run tests
2324

2425
jobs:
2526
tests:
26-
name: PHP ${{ matrix.php }}-redis-${{ matrix.redis }}
27-
28-
env:
29-
key: cache-v1
30-
31-
runs-on: ${{ matrix.os }}
32-
27+
name: PHP ${{ matrix.php }}
28+
runs-on: ubuntu-latest
3329
strategy:
30+
fail-fast: false
3431
matrix:
35-
os:
36-
- ubuntu-latest
37-
38-
php:
39-
- 8.0
40-
- 8.1
41-
42-
redis:
43-
- 4
44-
- 5
45-
- 6
46-
32+
php: [ '8.0', '8.1' ]
4733
steps:
4834
- name: Checkout
4935
uses: actions/checkout@v2
5036

51-
- name: Start Redis v${{ matrix.redis }}
52-
uses: superchargejs/redis-github-action@1.2.0
53-
with:
54-
redis-version: ${{ matrix.redis }}
55-
56-
- name: Install PHP
57-
uses: shivammathur/setup-php@v2
58-
with:
59-
php-version: ${{ matrix.php }}
60-
ini-values: date.timezone='UTC'
61-
coverage: pcov
62-
tools: composer:v2
63-
64-
- name: Determine composer cache directory
65-
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV
66-
67-
- name: Cache dependencies installed with composer
68-
uses: actions/cache@v2
69-
with:
70-
path: ${{ env.COMPOSER_CACHE_DIR }}
71-
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
72-
restore-keys: |
73-
php${{ matrix.php }}-composer-
74-
75-
- name: Update composer
76-
run: composer self-update
77-
78-
- name: Install dependencies with composer
79-
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
37+
- name: Build
38+
run: make build v=${{ matrix.php }}
8039

8140
- name: Run tests with phpunit
82-
run: vendor/bin/phpunit --colors=always
41+
run: make test v=${{ matrix.php }}

.github/workflows/mutation.yml

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
- 'psalm.xml'
1010

1111
push:
12+
branches: [ 'master' ]
1213
paths-ignore:
1314
- 'docs/**'
1415
- 'README.md'
@@ -17,7 +18,7 @@ on:
1718
- '.gitattributes'
1819
- 'psalm.xml'
1920

20-
name: mutation test
21+
name: Mutation test
2122

2223
jobs:
2324
mutation:
@@ -31,44 +32,16 @@ jobs:
3132
- ubuntu-latest
3233

3334
php:
34-
- 8.1
35+
- '8.0'
36+
- '8.1'
3537

3638
steps:
3739
- name: Checkout
3840
uses: actions/checkout@v2
3941

40-
- name: Start Redis v6
41-
uses: superchargejs/redis-github-action@1.2.0
42-
with:
43-
redis-version: 6
44-
45-
- name: Install PHP
46-
uses: shivammathur/setup-php@v2
47-
with:
48-
php-version: ${{ matrix.php }}
49-
ini-values: memory_limit=-1
50-
coverage: pcov
51-
tools: composer:v2
52-
53-
- name: Determine composer cache directory
54-
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV
55-
56-
- name: Cache dependencies installed with composer
57-
uses: actions/cache@v2
58-
with:
59-
path: ${{ env.COMPOSER_CACHE_DIR }}
60-
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
61-
restore-keys: |
62-
php${{ matrix.php }}-composer-
63-
64-
- name: Update composer
65-
run: composer self-update
66-
67-
- name: Install dependencies with composer
68-
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
69-
7042
- name: Run infection
7143
run: |
72-
vendor/bin/roave-infection-static-analysis-plugin -j2 --ignore-msi-with-no-mutations --only-covered
44+
make build v=${{ matrix.php }}
45+
make mutation-test v=${{ matrix.php }}
7346
env:
7447
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}

.github/workflows/static.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
- 'phpunit.xml.dist'
1111

1212
push:
13+
branches: [ 'master' ]
1314
paths-ignore:
1415
- 'docs/**'
1516
- 'README.md'

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ phpunit.phar
2828
/phpunit.xml
2929
# phpunit cache
3030
.phpunit.result.cache
31+
# phpunit coverage report
32+
coverage.xml

.scrutinizer.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,23 @@ filter:
88
build:
99
image: default-bionic
1010

11-
environment:
12-
php:
13-
version: 8.0.11
14-
ini:
15-
"xdebug.mode": coverage
16-
1711
nodes:
1812
analysis:
1913
tests:
2014
override:
2115
- php-scrutinizer-run
2216

2317
phpunit:
18+
environment:
19+
docker: true
2420
dependencies:
2521
override:
26-
- composer self-update
27-
- composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
22+
- make build
23+
- make test v=8.1
2824

2925
tests:
3026
override:
31-
- command: "./vendor/bin/phpunit --coverage-clover ./coverage.xml"
27+
- command: make build && make create-cluster && make coverage
3228
on_node: 1
3329
coverage:
3430
file: coverage.xml

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## 2.0.0 under development
44

55
- Chg #5: Raise the minimum `psr/simple-cache` version to `^2.0|^3.0` and the minimum PHP version to `^8.0` (@dehbka)
6+
- Enh #7: Cluster support added (s1lver)
7+
- Chg #7: Raise the `predis/predis` version to `^2.1`
68

79
## 1.0.0 November 07, 2021
810

Makefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
help: ## Display help information
2+
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
3+
4+
build: ## Build an image from a docker-compose file. Params: {{ v=8.1 }}. Default latest PHP 8.1
5+
PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml up -d --build
6+
7+
down: ## Stop and remove containers, networks
8+
docker-compose -f tests/docker/docker-compose.yml down
9+
10+
start: ## Start services
11+
docker-compose -f tests/docker/docker-compose.yml up -d
12+
13+
sh: ## Enter the container with the application
14+
docker exec -it cache-redis-php sh
15+
16+
test: ## Run tests. Params: {{ v=8.1 }}. Default latest PHP 8.1
17+
PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml build --pull cache-redis-php
18+
make create-cluster
19+
PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml run cache-redis-php vendor/bin/phpunit --colors=always
20+
make down
21+
22+
create-cluster: ## Create Redis cluster
23+
docker exec redis1 sh -c "redis-cli -p 6381 -a Password --cluster create 172.20.128.2:6381 172.20.128.3:6382 172.20.128.4:6383 172.20.128.5:6384 172.20.128.6:6385 172.20.128.7:6386 --cluster-replicas 1 --no-auth-warning --cluster-yes"
24+
25+
connect-cluster: ## Connect to Redis cluster
26+
docker exec -it redis1 sh -c "redis-cli -c -p 6381 -a Password --no-auth-warning"
27+
28+
mutation-test: ## Run mutation tests. Params: {{ v=8.1 }}. Default latest PHP 8.1
29+
PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml build --pull cache-redis-php
30+
make create-cluster
31+
PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml run cache-redis-php php -dpcov.enabled=1 -dpcov.directory=. vendor/bin/roave-infection-static-analysis-plugin -j2 --ignore-msi-with-no-mutations --only-covered
32+
make down
33+
34+
coverage: ## Run code coverage. Params: {{ v=8.1 }}. Default latest PHP 8.1
35+
PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml run cache-redis-php vendor/bin/phpunit --coverage-clover coverage.xml
36+
make down
37+
38+
static-analyze: ## Run code static analyze. Params: {{ v=8.1 }}. Default latest PHP 8.1
39+
PHP_VERSION=$(filter-out $@,$(v)) docker-compose -f tests/docker/docker-compose.yml run cache-redis-php vendor/bin/psalm --config=psalm.xml --shepherd --stats --php-version=$(v)
40+
make down

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,63 @@ To work with values in a more efficient manner, batch operations should be used:
7979

8080
This package can be used as a cache handler for the [Yii Caching Library](https://github.com/yiisoft/cache).
8181

82+
## Redis cluster supported
83+
84+
The package implements [Redis cluster](https://redis.io/docs/management/scaling/) support via `Predis` package.
85+
86+
For example, if your cluster configuration has three master nodes and three slave nodes, your client configuration might look like this:
87+
88+
```php
89+
$client = new \Predis\Client([
90+
['host' => 'redis-node-1', 'port' => 'redis-node-port-1',],
91+
['host' => 'redis-node-2', 'port' => 'redis-node-port-2',],
92+
['host' => 'redis-node-3', 'port' => 'redis-node-port-3',],
93+
['host' => 'redis-node-4', 'port' => 'redis-node-port-4',],
94+
['host' => 'redis-node-5', 'port' => 'redis-node-port-5',],
95+
['host' => 'redis-node-6', 'port' => 'redis-node-port-6',],
96+
],
97+
[
98+
'cluster' => 'redis',
99+
'parameters' => [
100+
'password' => 'Password',
101+
],
102+
]
103+
);
104+
$cache = new \Yiisoft\Cache\Redis\RedisCache($client);
105+
```
106+
Predis will route commands on its own when specifying Redis nodes in the cluster to the appropriate nodes depending on the keys that are specified in the commands.
107+
108+
You can implement `\Predis\Distribution\DistributorInterface` to create their own distributors used by the client to distribute keys among a cluster of servers.
109+
110+
Then your config might look like this:
111+
112+
```php
113+
$client = new \Predis\Client([
114+
['host' => 'redis-node-1', 'port' => 'redis-node-port-1',],
115+
['host' => 'redis-node-2', 'port' => 'redis-node-port-2',],
116+
['host' => 'redis-node-3', 'port' => 'redis-node-port-3',],
117+
['host' => 'redis-node-4', 'port' => 'redis-node-port-4',],
118+
['host' => 'redis-node-5', 'port' => 'redis-node-port-5',],
119+
['host' => 'redis-node-6', 'port' => 'redis-node-port-6',],
120+
],
121+
[
122+
'cluster' => static function () {
123+
$distributor = new \CustomDistributor(); // Your custom distributor
124+
$strategy = new \Predis\Cluster\PredisStrategy($distributor);
125+
126+
return new \Predis\Connection\Cluster\PredisCluster($strategy);
127+
},
128+
'parameters' => [
129+
'password' => 'Password',
130+
],
131+
]
132+
);
133+
```
134+
82135
## Testing
83136

137+
> The tests use a connection to a running Redis cluster. If you are not using Docker, you must start the cluster yourself before running the tests.
138+
84139
### Unit testing
85140

86141
The package is tested with [PHPUnit](https://phpunit.de/). To run tests:
@@ -106,6 +161,29 @@ The code is statically analyzed with [Psalm](https://psalm.dev/). To run static
106161
./vendor/bin/psalm
107162
```
108163

164+
## Testing in Docker
165+
166+
### Prepare
167+
168+
```bash
169+
# {{ v }} = 8.0, 8.1, 8.2. Default PHP 8.1
170+
make build v=8.1
171+
```
172+
173+
### Unit testing
174+
175+
```bash
176+
# {{ v }} = 8.0, 8.1, 8.2. Default PHP 8.1
177+
make test v=8.1
178+
```
179+
180+
### Mutation testing
181+
182+
```bash
183+
# {{ v }} = 8.0, 8.1, 8.2. Default PHP 8.1
184+
make mutation-test v=8.0
185+
```
186+
109187
## License
110188

111189
The Yii Caching Library - Redis Handler is free software. It is released under the terms of the BSD License.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
],
3232
"require": {
3333
"php": "^8.0",
34-
"predis/predis": "^1.1",
34+
"predis/predis": "^2.1",
3535
"psr/simple-cache": "^2.0|^3.0"
3636
},
3737
"require-dev": {

0 commit comments

Comments
 (0)