@@ -37,6 +37,7 @@ It enables you to set and query its data or use its PubSub topics to react to in
37
37
* [ createClient()] ( #createclient )
38
38
* [ createLazyClient()] ( #createlazyclient )
39
39
* [ Client] ( #client )
40
+ * [ __ call()] ( #__call )
40
41
* [ end()] ( #end )
41
42
* [ close()] ( #close )
42
43
* [ error event] ( #error-event )
@@ -86,7 +87,8 @@ See also the [examples](examples).
86
87
87
88
### Commands
88
89
89
- All [ Redis commands] ( https://redis.io/commands ) are automatically available as public methods like this:
90
+ Most importantly, this project provides a [ ` Client ` ] ( #client ) instance that
91
+ can be used to invoke all [ Redis commands] ( https://redis.io/commands ) (such as ` GET ` , ` SET ` , etc.).
90
92
91
93
``` php
92
94
$client->get($key);
@@ -107,26 +109,41 @@ $client->select($database);
107
109
// many more…
108
110
```
109
111
110
- Listing all available commands is out of scope here, please refer to the [ Redis command reference ] ( https://redis.io/commands ) .
111
- All [ Redis commands ] ( https://redis.io/commands ) are automatically available as public methods via the magic ` __call() ` method .
112
+ Each method call matches the respective [ Redis command] ( https://redis.io/commands ) .
113
+ For example, the ` $redis->get() ` method will invoke the [ ` GET ` command ] ( https://redis.io/commands/get ) .
112
114
113
- Each of these commands supports async operation and either * resolves* with
114
- its * results* or * rejects* with an ` Exception ` .
115
- Please see the following section about [ promises] ( #promises ) for more details.
115
+ All [ Redis commands] ( https://redis.io/commands ) are automatically available as
116
+ public methods via the magic [ ` __call() ` method] ( #__call ) .
117
+ Listing all available commands is out of scope here, please refer to the
118
+ [ Redis command reference] ( https://redis.io/commands ) .
119
+
120
+ Any arguments passed to the method call will be forwarded as command arguments.
121
+ For example, the ` $redis->set('name', 'Alice') ` call will perform the equivalent of a
122
+ ` SET name Alice ` command. It's safe to pass integer arguments where applicable (for
123
+ example ` $redis->expire($key, 60) ` ), but internally Redis requires all arguments to
124
+ always be coerced to string values.
125
+
126
+ Each of these commands supports async operation and returns a [ Promise] ( #promises )
127
+ that eventually * fulfills* with its * results* on success or * rejects* with an
128
+ ` Exception ` on error. See also the following section about [ promises] ( #promises )
129
+ for more details.
116
130
117
131
### Promises
118
132
119
- Sending commands is async (non-blocking), so you can actually send multiple commands in parallel.
120
- Redis will respond to each command request with a response message, pending commands will be pipelined automatically.
133
+ Sending commands is async (non-blocking), so you can actually send multiple
134
+ commands in parallel.
135
+ Redis will respond to each command request with a response message, pending
136
+ commands will be pipelined automatically.
121
137
122
- Sending commands uses a [ Promise] ( https://github.com/reactphp/promise ) -based interface that makes it easy to react to when a command is * fulfilled*
123
- (i.e. either successfully resolved or rejected with an error):
138
+ Sending commands uses a [ Promise] ( https://github.com/reactphp/promise ) -based
139
+ interface that makes it easy to react to when a command is completed
140
+ (i.e. either successfully fulfilled or rejected with an error):
124
141
125
142
``` php
126
- $client->set('hello', 'world');
127
- $client->get('hello')->then(function ($response) {
128
- // response received for GET command
129
- echo 'hello ' . $response ;
143
+ $redis->get($key)->then(function (string $value) {
144
+ var_dump($value);
145
+ }, function (Exception $e) {
146
+ echo 'Error: ' . $e->getMessage() . PHP_EOL ;
130
147
});
131
148
```
132
149
@@ -510,6 +527,38 @@ and keeps track of pending commands.
510
527
Besides defining a few methods, this interface also implements the
511
528
` EventEmitterInterface ` which allows you to react to certain events as documented below.
512
529
530
+ #### __ call()
531
+
532
+ The ` __call(string $name, string[] $args): PromiseInterface<mixed,Exception> ` method can be used to
533
+ invoke the given command.
534
+
535
+ This is a magic method that will be invoked when calling any Redis command on this instance.
536
+ Each method call matches the respective [ Redis command] ( https://redis.io/commands ) .
537
+ For example, the ` $redis->get() ` method will invoke the [ ` GET ` command] ( https://redis.io/commands/get ) .
538
+
539
+ ``` php
540
+ $redis->get($key)->then(function (string $value) {
541
+ var_dump($value);
542
+ }, function (Exception $e) {
543
+ echo 'Error: ' . $e->getMessage() . PHP_EOL;
544
+ });
545
+ ```
546
+
547
+ All [ Redis commands] ( https://redis.io/commands ) are automatically available as
548
+ public methods via this magic ` __call() ` method.
549
+ Listing all available commands is out of scope here, please refer to the
550
+ [ Redis command reference] ( https://redis.io/commands ) .
551
+
552
+ Any arguments passed to the method call will be forwarded as command arguments.
553
+ For example, the ` $redis->set('name', 'Alice') ` call will perform the equivalent of a
554
+ ` SET name Alice ` command. It's safe to pass integer arguments where applicable (for
555
+ example ` $redis->expire($key, 60) ` ), but internally Redis requires all arguments to
556
+ always be coerced to string values.
557
+
558
+ Each of these commands supports async operation and returns a [ Promise] ( #promises )
559
+ that eventually * fulfills* with its * results* on success or * rejects* with an
560
+ ` Exception ` on error. See also [ promises] ( #promises ) for more details.
561
+
513
562
#### end()
514
563
515
564
The ` end():void ` method can be used to
0 commit comments