@@ -47,23 +47,22 @@ local Redis server and send some requests:
4747$loop = React\EventLoop\Factory::create();
4848$factory = new Factory($loop);
4949
50- $factory->createClient('localhost')->then(function (Client $client) use ($loop) {
51- $client->set('greeting', 'Hello world');
52- $client->append('greeting', '!');
53-
54- $client->get('greeting')->then(function ($greeting) {
55- // Hello world!
56- echo $greeting . PHP_EOL;
57- });
58-
59- $client->incr('invocation')->then(function ($n) {
60- echo 'This is invocation #' . $n . PHP_EOL;
61- });
62-
63- // end connection once all pending requests have been resolved
64- $client->end();
50+ $client = $factory->createLazyClient('localhost');
51+ $client->set('greeting', 'Hello world');
52+ $client->append('greeting', '!');
53+
54+ $client->get('greeting')->then(function ($greeting) {
55+ // Hello world!
56+ echo $greeting . PHP_EOL;
57+ });
58+
59+ $client->incr('invocation')->then(function ($n) {
60+ echo 'This is invocation #' . $n . PHP_EOL;
6561});
6662
63+ // end connection once all pending requests have been resolved
64+ $client->end();
65+
6766$loop->run();
6867```
6968
@@ -101,7 +100,7 @@ $factory = new Factory($loop, $connector);
101100
102101#### createClient()
103102
104- The ` createClient($redisUri): PromiseInterface<Client,Exception> ` method can be used to
103+ The ` createClient(string $redisUri): PromiseInterface<Client,Exception> ` method can be used to
105104create a new [ ` Client ` ] ( #client ) .
106105
107106It helps with establishing a plain TCP/IP or secure TLS connection to Redis
@@ -198,9 +197,103 @@ $factory->createClient('localhost?timeout=0.5');
198197
199198#### createLazyClient()
200199
201- The ` createLazyClient($redisUri) ` method can be used to create a new [ ` Client ` ] ( #client ) which lazily
202- creates and connects to the configured redis server on the first command. Internally it will use ` createClient() `
203- when the first command comes in, queues all commands while connecting, and pass on all commands directly when connected.
200+ The ` createLazyClient(string $redisUri): Client ` method can be used to
201+ create a new [ ` Client ` ] ( #client ) .
202+
203+ It helps with establishing a plain TCP/IP or secure TLS connection to Redis
204+ and optionally authenticating (AUTH) and selecting the right database (SELECT).
205+
206+ ``` php
207+ $client = $factory->createLazyClient('redis://localhost:6379');
208+
209+ $client->incr('hello');
210+ $client->end();
211+ ```
212+
213+ This method immediately returns a "virtual" connection implementing the
214+ [ ` Client ` ] ( #client ) that can be used to interface with your Redis database.
215+ Internally, it lazily creates the underlying database connection (which may
216+ take some time) only once the first request is invoked on this instance and
217+ will queue all outstanding requests until the underlying connection is ready.
218+
219+ From a consumer side this means that you can start sending commands to the
220+ database right away while the actual connection may still be outstanding.
221+ It will ensure that all commands will be executed in the order they are
222+ enqueued once the connection is ready. If the database connection fails,
223+ it will emit an ` error ` event, reject all outstanding commands and ` close `
224+ the connection as described in the ` Client ` . In other words, it behaves just
225+ like a real connection and frees you from having to deal with its async
226+ resolution.
227+
228+ Note that creating the underlying connection will be deferred until the
229+ first request is invoked. Accordingly, any eventual connection issues
230+ will be detected once this instance is first used. Similarly, calling
231+ ` end() ` on this instance before invoking any requests will succeed
232+ immediately and will not wait for an actual underlying connection.
233+
234+ Depending on your particular use case, you may prefer this method or the
235+ underlying ` createClient() ` which resolves with a promise. For many
236+ simple use cases it may be easier to create a lazy connection.
237+
238+ The ` $redisUri ` can be given in the
239+ [ standard] ( https://www.iana.org/assignments/uri-schemes/prov/redis ) form
240+ ` [redis[s]://][:auth@]host[:port][/db] ` .
241+ You can omit the URI scheme and port if you're connecting to the default port 6379:
242+
243+ ``` php
244+ // both are equivalent due to defaults being applied
245+ $factory->createLazyClient('localhost');
246+ $factory->createLazyClient('redis://localhost:6379');
247+ ```
248+
249+ Redis supports password-based authentication (` AUTH ` command). Note that Redis'
250+ authentication mechanism does not employ a username, so you can pass the
251+ password ` h@llo ` URL-encoded (percent-encoded) as part of the URI like this:
252+
253+ ``` php
254+ // all forms are equivalent
255+ $factory->createLazyClient('redis://:h%40llo@localhost');
256+ $factory->createLazyClient('redis://ignored:h%40llo@localhost');
257+ $factory->createLazyClient('redis://localhost?password=h%40llo');
258+ ```
259+
260+ You can optionally include a path that will be used to select (SELECT command) the right database:
261+
262+ ``` php
263+ // both forms are equivalent
264+ $factory->createLazyClient('redis://localhost/2');
265+ $factory->createLazyClient('redis://localhost?db=2');
266+ ```
267+
268+ You can use the [ standard] ( https://www.iana.org/assignments/uri-schemes/prov/rediss )
269+ ` rediss:// ` URI scheme if you're using a secure TLS proxy in front of Redis:
270+
271+ ``` php
272+ $factory->createLazyClient('rediss://redis.example.com:6340');
273+ ```
274+
275+ You can use the ` redis+unix:// ` URI scheme if your Redis instance is listening
276+ on a Unix domain socket (UDS) path:
277+
278+ ``` php
279+ $factory->createLazyClient('redis+unix:///tmp/redis.sock');
280+
281+ // the URI MAY contain `password` and `db` query parameters as seen above
282+ $factory->createLazyClient('redis+unix:///tmp/redis.sock?password=secret&db=2');
283+
284+ // the URI MAY contain authentication details as userinfo as seen above
285+ // should be used with care, also note that database can not be passed as path
286+ $factory->createLazyClient('redis+unix://:secret@/tmp/redis.sock');
287+ ```
288+
289+ This method respects PHP's ` default_socket_timeout ` setting (default 60s)
290+ as a timeout for establishing the underlying connection and waiting for
291+ successful authentication. You can explicitly pass a custom timeout value
292+ in seconds (or use a negative number to not apply a timeout) like this:
293+
294+ ``` php
295+ $factory->createLazyClient('localhost?timeout=0.5');
296+ ```
204297
205298### Client
206299
0 commit comments