Skip to content

Commit

Permalink
Consistently use $redis and fully qualified class names for Client
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Aug 29, 2021
1 parent 92d3208 commit a753f96
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 269 deletions.
76 changes: 38 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ local Redis server and send some requests:

```php
$factory = new Clue\React\Redis\Factory();
$client = $factory->createLazyClient('localhost:6379');
$redis = $factory->createLazyClient('localhost:6379');

$client->set('greeting', 'Hello world');
$client->append('greeting', '!');
$redis->set('greeting', 'Hello world');
$redis->append('greeting', '!');

$client->get('greeting')->then(function ($greeting) {
$redis->get('greeting')->then(function ($greeting) {
// Hello world!
echo $greeting . PHP_EOL;
});

$client->incr('invocation')->then(function ($n) {
$redis->incr('invocation')->then(function ($n) {
echo 'This is invocation #' . $n . PHP_EOL;
});

// end connection once all pending requests have been resolved
$client->end();
$redis->end();
```

See also the [examples](examples).
Expand All @@ -91,20 +91,20 @@ Most importantly, this project provides a [`Client`](#client) instance that
can be used to invoke all [Redis commands](https://redis.io/commands) (such as `GET`, `SET`, etc.).

```php
$client->get($key);
$client->set($key, $value);
$client->exists($key);
$client->expire($key, $seconds);
$client->mget($key1, $key2, $key3);
$redis->get($key);
$redis->set($key, $value);
$redis->exists($key);
$redis->expire($key, $seconds);
$redis->mget($key1, $key2, $key3);

$client->multi();
$client->exec();
$redis->multi();
$redis->exec();

$client->publish($channel, $payload);
$client->subscribe($channel);
$redis->publish($channel, $payload);
$redis->subscribe($channel);

$client->ping();
$client->select($database);
$redis->ping();
$redis->select($database);

// many more…
```
Expand Down Expand Up @@ -161,17 +161,17 @@ send a message to all clients currently subscribed to a given channel:
```php
$channel = 'user';
$message = json_encode(array('id' => 10));
$client->publish($channel, $message);
$redis->publish($channel, $message);
```

The [`SUBSCRIBE` command](https://redis.io/commands/subscribe) can be used to
subscribe to a channel and then receive incoming PubSub `message` events:

```php
$channel = 'user';
$client->subscribe($channel);
$redis->subscribe($channel);

$client->on('message', function ($channel, $payload) {
$redis->on('message', function ($channel, $payload) {
// pubsub message received on given $channel
var_dump($channel, json_decode($payload));
});
Expand All @@ -181,9 +181,9 @@ Likewise, you can use the same client connection to subscribe to multiple
channels by simply executing this command multiple times:

```php
$client->subscribe('user.register');
$client->subscribe('user.join');
$client->subscribe('user.leave');
$redis->subscribe('user.register');
$redis->subscribe('user.join');
$redis->subscribe('user.leave');
```

Similarly, the [`PSUBSCRIBE` command](https://redis.io/commands/psubscribe) can
Expand All @@ -193,9 +193,9 @@ all incoming PubSub messages with the `pmessage` event:

```php
$pattern = 'user.*';
$client->psubscribe($pattern);
$redis->psubscribe($pattern);

$client->on('pmessage', function ($pattern, $channel, $payload) {
$redis->on('pmessage', function ($pattern, $channel, $payload) {
// pubsub message received matching given $pattern
var_dump($channel, json_decode($payload));
});
Expand All @@ -213,10 +213,10 @@ receiving any further events for the given channel and pattern subscriptions
respectively:

```php
$client->subscribe('user');
$redis->subscribe('user');

Loop::addTimer(60.0, function () use ($client) {
$client->unsubscribe('user');
Loop::addTimer(60.0, function () use ($redis) {
$redis->unsubscribe('user');
});
```

Expand All @@ -235,16 +235,16 @@ Additionally, can listen for the following PubSub events to get notifications
about subscribed/unsubscribed channels and patterns:

```php
$client->on('subscribe', function ($channel, $total) {
$redis->on('subscribe', function ($channel, $total) {
// subscribed to given $channel
});
$client->on('psubscribe', function ($pattern, $total) {
$redis->on('psubscribe', function ($pattern, $total) {
// subscribed to matching given $pattern
});
$client->on('unsubscribe', function ($channel, $total) {
$redis->on('unsubscribe', function ($channel, $total) {
// unsubscribed from given $channel
});
$client->on('punsubscribe', function ($pattern, $total) {
$redis->on('punsubscribe', function ($pattern, $total) {
// unsubscribed from matching given $pattern
});
```
Expand Down Expand Up @@ -299,7 +299,7 @@ and optionally authenticating (AUTH) and selecting the right database (SELECT).

```php
$factory->createClient('localhost:6379')->then(
function (Client $client) {
function (Client $redis) {
// client connected (and authenticated)
},
function (Exception $e) {
Expand Down Expand Up @@ -395,10 +395,10 @@ It helps with establishing a plain TCP/IP or secure TLS connection to Redis
and optionally authenticating (AUTH) and selecting the right database (SELECT).

```php
$client = $factory->createLazyClient('localhost:6379');
$redis = $factory->createLazyClient('localhost:6379');

$client->incr('hello');
$client->end();
$redis->incr('hello');
$redis->end();
```

This method immediately returns a "virtual" connection implementing the
Expand Down Expand Up @@ -576,7 +576,7 @@ when the client connection is lost or is invalid.
The event receives a single `Exception` argument for the error instance.

```php
$client->on('error', function (Exception $e) {
$redis->on('error', function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
});
```
Expand All @@ -590,7 +590,7 @@ errors caused by invalid commands.
The `close` event will be emitted once the client connection closes (terminates).

```php
$client->on('close', function () {
$redis->on('close', function () {
echo 'Connection closed' . PHP_EOL;
});
```
Expand Down
17 changes: 7 additions & 10 deletions examples/cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,23 @@
// $ php examples/cli.php
// $ REDIS_URI=localhost:6379 php examples/cli.php

use Clue\React\Redis\Client;
use Clue\React\Redis\Factory;
use React\EventLoop\Loop;
use React\Promise\PromiseInterface;

require __DIR__ . '/../vendor/autoload.php';

$factory = new Factory();
$factory = new Clue\React\Redis\Factory();

echo '# connecting to redis...' . PHP_EOL;

$factory->createClient(getenv('REDIS_URI') ?: 'localhost:6379')->then(function (Client $client) {
$factory->createClient(getenv('REDIS_URI') ?: 'localhost:6379')->then(function (Clue\React\Redis\Client $redis) {
echo '# connected! Entering interactive mode, hit CTRL-D to quit' . PHP_EOL;

Loop::addReadStream(STDIN, function () use ($client) {
Loop::addReadStream(STDIN, function () use ($redis) {
$line = fgets(STDIN);
if ($line === false || $line === '') {
echo '# CTRL-D -> Ending connection...' . PHP_EOL;
Loop::removeReadStream(STDIN);
return $client->end();
return $redis->end();
}

$line = rtrim($line);
Expand All @@ -32,10 +29,10 @@

$params = explode(' ', $line);
$method = array_shift($params);
$promise = call_user_func_array(array($client, $method), $params);
$promise = call_user_func_array(array($redis, $method), $params);

// special method such as end() / close() called
if (!$promise instanceof PromiseInterface) {
if (!$promise instanceof React\Promise\PromiseInterface) {
return;
}

Expand All @@ -46,7 +43,7 @@
});
});

$client->on('close', function() {
$redis->on('close', function() {
echo '## DISCONNECTED' . PHP_EOL;

Loop::removeReadStream(STDIN);
Expand Down
12 changes: 5 additions & 7 deletions examples/incr.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
// $ php examples/incr.php
// $ REDIS_URI=localhost:6379 php examples/incr.php

use Clue\React\Redis\Factory;

require __DIR__ . '/../vendor/autoload.php';

$factory = new Factory();
$client = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
$factory = new Clue\React\Redis\Factory();
$redis = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');

$client->incr('test');
$redis->incr('test');

$client->get('test')->then(function ($result) {
$redis->get('test')->then(function ($result) {
var_dump($result);
}, function (Exception $e) {
echo 'Error: ' . $e->getMessage() . PHP_EOL;
exit(1);
});

//$client->end();
//$redis->end();
10 changes: 4 additions & 6 deletions examples/publish.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
// $ php examples/publish.php
// $ REDIS_URI=localhost:6379 php examples/publish.php channel message

use Clue\React\Redis\Factory;

require __DIR__ . '/../vendor/autoload.php';

$factory = new Factory();
$client = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
$factory = new Clue\React\Redis\Factory();
$redis = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');

$channel = isset($argv[1]) ? $argv[1] : 'channel';
$message = isset($argv[2]) ? $argv[2] : 'message';

$client->publish($channel, $message)->then(function ($received) {
$redis->publish($channel, $message)->then(function ($received) {
echo 'Successfully published. Received by ' . $received . PHP_EOL;
}, function (Exception $e) {
echo 'Unable to publish: ' . $e->getMessage() . PHP_EOL;
exit(1);
});

$client->end();
$redis->end();
19 changes: 9 additions & 10 deletions examples/subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@
// $ php examples/subscribe.php
// $ REDIS_URI=localhost:6379 php examples/subscribe.php channel

use Clue\React\Redis\Factory;
use React\EventLoop\Loop;

require __DIR__ . '/../vendor/autoload.php';

$factory = new Factory();
$client = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
$factory = new Clue\React\Redis\Factory();
$redis = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');

$channel = isset($argv[1]) ? $argv[1] : 'channel';

$client->subscribe($channel)->then(function () {
$redis->subscribe($channel)->then(function () {
echo 'Now subscribed to channel ' . PHP_EOL;
}, function (Exception $e) use ($client) {
$client->close();
}, function (Exception $e) use ($redis) {
$redis->close();
echo 'Unable to subscribe: ' . $e->getMessage() . PHP_EOL;
});

$client->on('message', function ($channel, $message) {
$redis->on('message', function ($channel, $message) {
echo 'Message on ' . $channel . ': ' . $message . PHP_EOL;
});

// automatically re-subscribe to channel on connection issues
$client->on('unsubscribe', function ($channel) use ($client) {
$redis->on('unsubscribe', function ($channel) use ($redis) {
echo 'Unsubscribed from ' . $channel . PHP_EOL;

Loop::addPeriodicTimer(2.0, function ($timer) use ($client, $channel){
$client->subscribe($channel)->then(function () use ($timer) {
Loop::addPeriodicTimer(2.0, function ($timer) use ($redis, $channel){
$redis->subscribe($channel)->then(function () use ($timer) {
echo 'Now subscribed again' . PHP_EOL;
Loop::cancelTimer($timer);
}, function (Exception $e) {
Expand Down
24 changes: 12 additions & 12 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ public function createClient($uri)
$pass = isset($args['password']) ? $args['password'] : (isset($parts['pass']) ? rawurldecode($parts['pass']) : null);
if (isset($args['password']) || isset($parts['pass'])) {
$pass = isset($args['password']) ? $args['password'] : rawurldecode($parts['pass']);
$promise = $promise->then(function (StreamingClient $client) use ($pass, $uri) {
return $client->auth($pass)->then(
function () use ($client) {
return $client;
$promise = $promise->then(function (StreamingClient $redis) use ($pass, $uri) {
return $redis->auth($pass)->then(
function () use ($redis) {
return $redis;
},
function (\Exception $e) use ($client, $uri) {
$client->close();
function (\Exception $e) use ($redis, $uri) {
$redis->close();

$const = '';
$errno = $e->getCode();
Expand All @@ -131,13 +131,13 @@ function (\Exception $e) use ($client, $uri) {
// use `?db=1` query or `/1` path (skip first slash)
if (isset($args['db']) || (isset($parts['path']) && $parts['path'] !== '/')) {
$db = isset($args['db']) ? $args['db'] : substr($parts['path'], 1);
$promise = $promise->then(function (StreamingClient $client) use ($db, $uri) {
return $client->select($db)->then(
function () use ($client) {
return $client;
$promise = $promise->then(function (StreamingClient $redis) use ($db, $uri) {
return $redis->select($db)->then(
function () use ($redis) {
return $redis;
},
function (\Exception $e) use ($client, $uri) {
$client->close();
function (\Exception $e) use ($redis, $uri) {
$redis->close();

$const = '';
$errno = $e->getCode();
Expand Down
Loading

0 comments on commit a753f96

Please sign in to comment.