Skip to content

Commit f531216

Browse files
authored
v1.6.0
v1.6.0
2 parents eedba71 + 0c41eae commit f531216

File tree

168 files changed

+3905
-1885
lines changed

Some content is hidden

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

168 files changed

+3905
-1885
lines changed

.travis.yml

+21-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ matrix:
1515
services:
1616
- docker
1717

18+
install:
19+
- composer install
20+
1821
before_install:
1922
- docker pull cheprasov/redis-for-tests:latest;
23+
- docker pull cheprasov/redis-cluster-for-tests:latest;
24+
2025
- |
2126
docker run -i -t -d \
2227
-p 127.0.0.1:6381:6381 \
@@ -27,9 +32,22 @@ before_install:
2732
-p 127.0.0.1:6386:6386 \
2833
-p 127.0.0.1:6387:6387 \
2934
-p 127.0.0.1:6388:6388 \
35+
-p 127.0.0.1:6389:6389 \
36+
-p 127.0.0.1:6390:6390 \
3037
cheprasov/redis-for-tests
38+
39+
- |
40+
docker run -i -t -d \
41+
-p 127.0.0.1:7001:7001 \
42+
-p 127.0.0.1:7002:7002 \
43+
-p 127.0.0.1:7003:7003 \
44+
-p 127.0.0.1:7004:7004 \
45+
-p 127.0.0.1:7005:7005 \
46+
-p 127.0.0.1:7006:7006 \
47+
cheprasov/redis-cluster-for-tests
48+
3149
- docker ps;
32-
- sleep 2;
50+
- sleep 5;
3351

34-
install:
35-
- composer install
52+
before_script:
53+
- if [[ $TRAVIS_PHP_VERSION = 'hhvm' ]] ; then rm ./tests/Unit/Client/AbstractRedisClientIsolatedTest.php; fi;

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
## CHANGELOG
22

3+
### v1.6.0 (2017-01-07)
4+
- Added support for Redis 4.0 (the Client was tested with Redis 4.0 RC2).
5+
- Added support for Redis Cluster.
6+
- Added method **_syncClusterSlotsFromRedisServer** for RedisClient.
7+
- Added command **SWAPDB**, **UNLINK**, **MEMORY** for Redis >= 4.0
8+
- Updated command **FLUSHALL**, **FLUSHDB** for Redis >= 4.0
9+
310
### v1.5.1 (2016-08-17)
411
- Fixed critical bug: https://github.com/cheprasov/php-redis-client/pull/45 Thanks to @BrianFranklin for help.
512

README.md

+70-34
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
22
[![Latest Stable Version](https://poser.pugx.org/cheprasov/php-redis-client/v/stable)](https://packagist.org/packages/cheprasov/php-redis-client)
33
[![Total Downloads](https://poser.pugx.org/cheprasov/php-redis-client/downloads)](https://packagist.org/packages/cheprasov/php-redis-client)
4-
# RedisClient v1.5.1 for PHP >= 5.5
4+
# RedisClient v1.6.0 for PHP >= 5.5
55

66
## About
7-
RedisClient is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from __2.6__ to __3.2__
7+
RedisClient is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from __2.6__ to __4.0__
88

99
## Main features
10-
- Support Redis versions from __2.6__ to __3.2.x__.
10+
- Support Redis versions from __2.6.x__ to __4.0.x__.
1111
- Support __TCP/IP__ and __UNIX__ sockets.
1212
- Support __PubSub__ and __Monitor__ functionallity.
1313
- Support __Pipeline__ and __Transactions__.
14+
- Support __Redis Cluster__.
1415
- Support __RAW__ commands as strings `"SET foo bar"` or as arrays `['SET', 'foo', 'bar']`.
1516
- Connections to Redis are established lazily by the client upon the first command.
1617
- Easy to use with IDE, client has PHPDocs for all supported versions.
17-
- By default, the client works with the latest stable version of Redis (3.2.x).
18+
- By default, the client works with the latest stable version of Redis (3.2).
1819
- About **6.5-8.5% faster** than predis (based on this test: https://github.com/cheprasov/php-redis-client-vs-predis-test)
19-
- Client was tested on the next versions of Redis: 2.6.17, 2.8.24, 3.0.7, 3.2.4 (and older).
20+
- Client was tested on the next versions of Redis: 2.6.17, 2.8.24, 3.0.7, 3.2.6, 4.0 RC2 (and other).
21+
- Also, Client was tested on PHP 5.5, 5.6, 7.0, 7.1, HHVM.
2022

2123
## Usage
2224

2325
### Config
2426

25-
```
26-
$Redis = ClientFactory::create([
27-
// Optional. Default = 'tcp://127.0.0.1:6379'. You can use 'unix:///tmp/redis.sock'
28-
'server' => 'tcp://127.0.0.1:6379',
27+
```php
28+
$config = [
29+
// Optional. Default = '127.0.0.1:6379'. You can use 'unix:///tmp/redis.sock'
30+
'server' => '127.0.0.1:6379',
2931

3032
// Optional. Default = 1
3133
'timeout' => 2,
@@ -36,62 +38,97 @@ $Redis = ClientFactory::create([
3638
// Optional. Use it only if Redis server requires password (AUTH)
3739
'password' => 'some-password',
3840

39-
// Use it, if you want to select not default db (db != 0) on connect
41+
// Optional. Use it, if you want to select not default db (db != 0) on connect
4042
'database' => 1,
41-
]);
43+
44+
// Optional. Array with configs for RedisCluster support
45+
'cluster' => [
46+
'enabled' => false,
47+
48+
// Optional. Default = []. Map of cluster slots and servers
49+
// array(max_slot => server [, ...])
50+
// Examples for Cluster with 3 Nodes:
51+
'clusters' => [
52+
5460 => '127.0.0.1:7001', // slots from 0 to 5460
53+
10922 => '127.0.0.1:7002', // slots from 5461 to 10922
54+
16383 => '127.0.0.1:7003', // slots from 10923 to 16383
55+
],
56+
57+
// Optional. Default = false.
58+
// Use the param to update cluster slot map below on init RedisClient.
59+
// RedisClient will execute command CLUSTER SLOTS to get map.
60+
'init_on_start' => false,
61+
62+
// Optional. Default = false.
63+
// If Redis returns error -MOVED then RedisClient will execute
64+
// command CLUSTER SLOTS to update cluster slot map
65+
'init_on_error_moved' => true,
66+
67+
// Optional. Defatult = 0.25 sec. It is timeout before next attempt on TRYAGAIN error.
68+
'timeout_on_error_tryagain' => 0.25, // sec
69+
]
70+
];
4271
```
4372

4473
### Create a new instance of RedisClient
4574
```php
4675
<?php
76+
namespace Examples;
77+
4778
require (dirname(__DIR__).'/vendor/autoload.php');
4879
// or require (dirname(__DIR__).'/src/autoloader.php');
4980

5081
use RedisClient\RedisClient;
5182
use RedisClient\Client\Version\RedisClient2x6;
83+
use RedisClient\ClientFactory;
5284

5385
// Example 1. Create new Instance for Redis version 2.8.x with config via factory
54-
5586
$Redis = ClientFactory::create([
56-
'server' => 'tcp://127.0.0.1:6379', // or 'unix:///tmp/redis.sock'
87+
'server' => '127.0.0.1:6379', // or 'unix:///tmp/redis.sock'
5788
'timeout' => 2,
5889
'version' => '2.8.24'
5990
]);
6091

61-
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL;
62-
// RedisClient: 2.8
92+
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 2.8
6393

6494

65-
// Example 2. Create new Instance for Redis version 2.6.x with config
95+
// Example 2. Create new Instance without config. Client will use default config.
96+
$Redis = new RedisClient();
97+
// By default, the client works with the latest stable version of Redis.
98+
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 3.2
99+
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL; // Redis: 3.0.3
66100

67-
$Redis = new RedisClient2x6([
68-
'server' => 'tcp://127.0.0.1:6379', // or 'unix:///tmp/redis.sock'
101+
102+
// Example 3. Create new Instance with config
103+
// By default, the client works with the latest stable version of Redis.
104+
$Redis = new RedisClient([
105+
'server' => '127.0.0.1:6387', // or 'unix:///tmp/redis.sock'
69106
'timeout' => 2
70107
]);
71108

72-
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL;
73-
// RedisClient: 2.6
109+
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 3.2
110+
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL; // Redis: 3.2.0
74111

75112

76-
// Example 3. Create new instance of client without factory
77-
78-
$Redis = new RedisClient([
113+
// Example 4. Create new Instance for Redis version 2.6.x with config
114+
$Redis = new RedisClient2x6([
79115
'server' => 'tcp://127.0.0.1:6379', // or 'unix:///tmp/redis.sock'
80116
'timeout' => 2
81117
]);
82118

83-
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL;
84-
echo 'Redis: '. $Redis->info('Server')['redis_version'] . PHP_EOL;
85-
86-
// By default, the client works with the latest stable version of Redis.
87-
// RedisClient: 3.2
88-
// Redis: 3.2.4
89-
119+
echo 'RedisClient: '. $Redis->getSupportedVersion() . PHP_EOL; // RedisClient: 2.6
90120

91121
```
92122
### Example
93123
Please, see examples here: https://github.com/cheprasov/php-redis-client/tree/master/examples
94124

125+
- [Create new instance](https://github.com/cheprasov/php-redis-client/tree/master/examples/create_new_instance.php)
126+
- [Using MONITOR](https://github.com/cheprasov/php-redis-client/tree/master/examples/monitor.php)
127+
- [Publish and Subscribe](https://github.com/cheprasov/php-redis-client/tree/master/examples/pubsub.php)
128+
- [Transactions](https://github.com/cheprasov/php-redis-client/tree/master/examples/transactions.php)
129+
- [Pipeline](https://github.com/cheprasov/php-redis-client/tree/master/examples/pipeline.php)
130+
- [Cluster support](https://github.com/cheprasov/php-redis-client/tree/master/examples/clusters.php)
131+
- [RAW Commands](https://github.com/cheprasov/php-redis-client/tree/master/examples/raw_commands.php)
95132

96133
## Installation
97134

@@ -107,13 +144,12 @@ and add dependency to your project:
107144

108145
## Running tests
109146

110-
1. Use Docker container with Redis for tests https://hub.docker.com/r/cheprasov/redis-for-tests/
111-
2. To run tests type in console (do not forget run Docker):
112-
147+
1. Run Docker container with Redis for tests https://hub.docker.com/r/cheprasov/redis-for-tests/
148+
2. Run Docker container with Redis Cluster for tests https://hub.docker.com/r/cheprasov/redis-cluster-for-tests/
149+
3. To run tests type in console:
113150

114151
./vendor/bin/phpunit
115152

116-
117153
## Something doesn't work
118154

119155
Feel free to fork project, fix bugs and finally request for pull

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "cheprasov/php-redis-client",
3-
"version": "1.5.1",
4-
"description": "Php client for Redis. It is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from 2.6 to 3.2",
3+
"version": "1.6.0",
4+
"description": "Php client for Redis. It is a fast, fully-functional and user-friendly client for Redis, optimized for performance. RedisClient supports the latest versions of Redis starting from 2.6 to 4.0",
55
"homepage": "http://github.com/cheprasov/php-redis-client",
66
"minimum-stability": "stable",
77
"license": "MIT",

examples/clusters.php

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* This file is part of RedisClient.
4+
* git: https://github.com/cheprasov/php-redis-client
5+
*
6+
* (C) Alexander Cheprasov <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/**
13+
* Cluster
14+
*/
15+
16+
namespace Examples;
17+
18+
require (dirname(__DIR__).'/vendor/autoload.php');
19+
// or require (dirname(__DIR__).'/src/autoloader.php');
20+
21+
use RedisClient\ClientFactory;
22+
use RedisClient\Cluster\ClusterMap;
23+
24+
// Example 1. Create RedisClient with Cluster support;
25+
$RedisClient = ClientFactory::create([
26+
'server' => '127.0.0.1:7001', // Default server for connection
27+
'cluster' => [
28+
'enabled' => true
29+
]
30+
]);
31+
print_r($RedisClient->clusterInfo());
32+
/*
33+
cluster_state:ok
34+
cluster_slots_assigned:16384
35+
cluster_slots_ok:16384
36+
cluster_slots_pfail:0
37+
cluster_slots_fail:0
38+
cluster_known_nodes:6
39+
cluster_size:3
40+
cluster_current_epoch:6
41+
cluster_my_epoch:1
42+
cluster_stats_messages_sent:472694
43+
cluster_stats_messages_received:472694
44+
*/
45+
46+
47+
// Example 2. Create RedisClient with Cluster Slot Map;
48+
$RedisClient = ClientFactory::create([
49+
'server' => '127.0.0.1:7001', // Default server for connection
50+
'cluster' => [
51+
'enabled' => true,
52+
'clusters' => [
53+
5460 => '127.0.0.1:7001', // slots from 0 to 5460
54+
10922 => '127.0.0.1:7002', // slots from 5461 to 10922
55+
16383 => '127.0.0.1:7003', // slots from 10923 to 16383
56+
],
57+
]
58+
]);
59+
60+
61+
// Example 3. Get Cluster Slot Map from Redis Server on init RedisClient
62+
$RedisClient = ClientFactory::create([
63+
'server' => '127.0.0.1:7001', // Default server for connection
64+
'cluster' => [
65+
'enabled' => true,
66+
// Use the param to update cluster slot map below on init RedisClient.
67+
// RedisClient will execute command CLUSTER SLOTS to get map.
68+
'init_on_start' => true,
69+
]
70+
]);
71+
72+
73+
// Example 4. Get Cluster Slot Map from Redis Server by command
74+
$RedisClient = ClientFactory::create([
75+
'server' => '127.0.0.1:7001', // Default server for connection
76+
'cluster' => [
77+
'enabled' => true,
78+
]
79+
]);
80+
// Use this command to sync Cluster Slot Map from Redis Server
81+
$RedisClient->_syncClusterSlotsFromRedisServer();
82+
83+
84+
// Example 5. Pseudo Redis Cluster.
85+
// If you use several Redis Servers without Cluster,
86+
// you can use RedisClient's cluster config just for sharding keys by the Cluster rulers.
87+
$RedisClient = ClientFactory::create([
88+
'server' => '127.0.0.1:6381', // Default server for connection
89+
'cluster' => [
90+
'enabled' => true,
91+
'clusters' => [
92+
5460 => '127.0.0.1:6381', // slots from 0 to 5460
93+
10922 => '127.0.0.1:6383', // slots from 5461 to 10922
94+
16383 => '127.0.0.1:6385', // slots from 10923 to 16383
95+
],
96+
]
97+
]);
98+
$RedisClient->set('foo', 'foo-42');
99+
echo ClusterMap::getSlotByKey('foo') . PHP_EOL; // 12182
100+
101+
$RedisClient->set('bar', 'bar-42');
102+
echo ClusterMap::getSlotByKey('bar') . PHP_EOL; // 5061
103+
104+
echo $RedisClient->get('foo') . PHP_EOL; // foo-42
105+
echo $RedisClient->get('bar') . PHP_EOL; // bar-42
106+
107+
// But, be careful with multi operation for pseudo Clusters,
108+
print_r($RedisClient->mget(['foo', 'bar'])); // ['foo-42', null]
109+
print_r($RedisClient->mget(['bar', 'foo'])); // ['bar-42', null]
110+
111+
112+
// Example 6. Each connection to Redis Server uses the same config.
113+
// For example, use can use password for all servers
114+
$RedisClient = ClientFactory::create([
115+
'server' => '127.0.0.1:6382', // Default server for connection
116+
'timeout' => 2,
117+
'password' => 'test-password-123',
118+
'cluster' => [
119+
'enabled' => true,
120+
'clusters' => [
121+
5460 => '127.0.0.1:6382', // slots from 0 to 5460
122+
10922 => '127.0.0.1:6384', // slots from 5461 to 10922
123+
16383 => '127.0.0.1:6386', // slots from 10923 to 16383
124+
],
125+
]
126+
]);
127+
$RedisClient->set('foo', 'foo-43');
128+
echo ClusterMap::getSlotByKey('foo') . PHP_EOL; // 12182
129+
130+
$RedisClient->set('bar', 'bar-43');
131+
echo ClusterMap::getSlotByKey('bar') . PHP_EOL; // 5061
132+
133+
echo $RedisClient->get('foo') . PHP_EOL; // foo-42
134+
echo $RedisClient->get('bar') . PHP_EOL; // bar-42
135+
136+

0 commit comments

Comments
 (0)