Skip to content

Commit fb95e31

Browse files
committed
Merge branch 'docs'
2 parents 277b540 + 62de83b commit fb95e31

File tree

2 files changed

+136
-3
lines changed

2 files changed

+136
-3
lines changed

README.md

+135-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
# clue/redis-react [![Build Status](https://travis-ci.org/clue/php-redis-react.svg?branch=master)](https://travis-ci.org/clue/php-redis-react)
22

3-
Async redis client implementation built on top of React PHP.
3+
Async [Redis](http://redis.io/) client implementation built on top of [React PHP](http://reactphp.org/).
4+
5+
[Redis](http://redis.io/) is an open source, advanced, in-memory key-value database.
6+
It offers a set of simple, atomic operations in order to work with its primitive data types.
7+
Its lightweight design and fast operation makes it an ideal candidate for modern application stacks.
8+
This library provides you a simple API to work with your Redis database from within PHP.
9+
It enables you to set and query its data or use its PubSub topics to react to incoming events.
10+
11+
* **Async execution of Commands** -
12+
Send any number commands to Redis in parallel (automatic pipeline) and
13+
process their responses as soon as results come in.
14+
The Promise-based design provides a *sane* interface to working with async responses.
15+
* **Event-driven core** -
16+
Register your event handler callbacks to react to incoming events, such as an incoming PubSub message or a MONITOR event.
17+
* **Lightweight, SOLID design** -
18+
Provides a thin abstraction that is [*just good enough*](http://en.wikipedia.org/wiki/Principle_of_good_enough)
19+
and does not get in your way.
20+
Future or custom commands and events require no changes to be supported.
21+
* **Good test coverage** -
22+
Comes with an automated tests suite and is regularly tested against versions as old as Redis v2.6+
423

524
> Note: This project is in beta stage! Feel free to report any issues you encounter.
625
726
## Quickstart example
827

928
Once [installed](#install), you can use the following code to connect to your
10-
local redis server and send some requests:
29+
local Redis server and send some requests:
1130

1231
```php
1332
$loop = React\EventLoop\Factory::create();
@@ -34,6 +53,120 @@ $loop->run();
3453

3554
See also the [examples](examples).
3655

56+
### Factory
57+
58+
The `Factory` is responsible for creating your `Client` instance.
59+
It also registers everything with the main `EventLoop`.
60+
61+
```php
62+
$loop = \React\EventLoop\Factory::create();
63+
$factory = new Factory($loop);
64+
```
65+
66+
The `createClient($redisUri)` method can be used to create a new `Client`.
67+
It helps with establishing a plain TCP/IP connection to Redis
68+
and optionally authenticating (AUTH) and selecting the right database (SELECT).
69+
70+
```php
71+
$factory->createClient('localhost')->then(
72+
function (Client $client) {
73+
// client connected and authenticated
74+
},
75+
function (Exception $e) {
76+
// an error occured while trying to connect or authorize client
77+
}
78+
);
79+
```
80+
81+
> Note: The given $redisUri *can* include a scheme, password, host, port and database definition.
82+
>
83+
> tcp://auth@localhost:6379/2
84+
85+
### Client
86+
87+
The `Client` is responsible for exchanging messages with Redis
88+
and keeps track of pending commands.
89+
90+
The `on($eventName, $eventHandler)` method can be used to register a new event handler.
91+
Incoming events and errors will be forwarded to registered event handler callbacks:
92+
93+
```php
94+
// global events:
95+
$client->on('data', function (MessageInterface $message) {
96+
// process an incoming message (raw message object)
97+
});
98+
$client->on('close', function () {
99+
// the connection to Redis just closed
100+
});
101+
$client->on('error', function (Exception $e) {
102+
// and error has just been detected, the connection will terminate...
103+
});
104+
105+
// pubsub events:
106+
$client->on('message', function ($channel, $payload) {
107+
// pubsub message received on given $channel
108+
});
109+
$client->on('pmessage', function ($pattern, $payload) {
110+
// pubsub message received matching given $pattern
111+
});
112+
$client->on('subscribe', function ($channel, $total) {
113+
// subscribed to given $channel
114+
});
115+
$client->on('psubscribe', function ($pattern, $total) {
116+
// subscribed to matching given $pattern
117+
});
118+
$client->on('unsubscribe', function ($channel, $total) {
119+
// unsubscribed from given $channel
120+
});
121+
$client->on('punsubscribe', function ($pattern, $total) {
122+
// unsubscribed from matching given $pattern
123+
});
124+
125+
// monitor events:
126+
$client->on('monitor', function (StatusReply $message) {
127+
// somebody executed a command
128+
});
129+
```
130+
131+
Sending commands is async (non-blocking), so you can actually send multiple commands in parallel.
132+
Redis will respond to each command request with a response message.
133+
Sending commands uses a Promise-based interface that makes it easy to react to when a command is *fulfilled*
134+
(i.e. either successfully resolved or rejected with an error):
135+
136+
```php
137+
$client->set('hello', 'world');
138+
$client->get('hello')->then(function ($response) {
139+
// response received for GET command
140+
echo 'hello ' . $response;
141+
});
142+
```
143+
144+
All [Redis commands](http://redis.io/commands) are automatically available as public methods (via the magic `__call()` method) like this:
145+
146+
```php
147+
$client->get($key);
148+
$client->set($key, $value);
149+
$client->exists($key);
150+
$client->expire($key, $seconds);
151+
$client->mget($key1, $key2, $key3);
152+
153+
$client->multi();
154+
$client->exec();
155+
156+
$client->publish($channel, $payload);
157+
$client->subscribe($channel);
158+
159+
$client->ping();
160+
$client->select($database);
161+
```
162+
163+
Listing all available commands is out of scope here, please refer to the [Redis command reference](http://redis.io/commands).
164+
165+
The `close()` method can be used to force-close the Redis connection and reject all pending commands.
166+
167+
The `end()` method can be used to soft-close the Redis connection once all pending commands are completed.
168+
169+
37170
## Install
38171

39172
The recommended way to install this library is [through composer](http://getcomposer.org). [New to composer?](http://getcomposer.org/doc/00-intro.md)
@@ -49,4 +182,3 @@ The recommended way to install this library is [through composer](http://getcomp
49182
## License
50183

51184
MIT
52-

src/Client.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Simple interface for executing redis commands
1111
*
1212
* @event data(ModelInterface $messageModel, Client $thisClient)
13+
* @event error(Exception $error)
1314
* @event close()
1415
*
1516
* @event message($channel, $message)

0 commit comments

Comments
 (0)