@@ -116,6 +116,92 @@ const size_t argvlen[] = {3, 8, 5};
116
116
redisReply *reply = redisCommandArgv(c, argc, argv, argvlen);
117
117
```
118
118
119
+ ## Construct asynchronous commands
120
+
121
+ Use the ` redisAsyncCommand() ` and ` redisAsyncCommandArgv() `
122
+ functions to send commands to the server asynchronously:
123
+
124
+ ``` c
125
+ int redisAsyncCommand (
126
+ redisAsyncContext * ac, redisCallbackFn * fn, void * privdata,
127
+ const char * format, ...);
128
+ int redisAsyncCommandArgv(
129
+ redisAsyncContext * ac, redisCallbackFn * fn, void * privdata,
130
+ int argc, const char ** argv, const size_t * argvlen);
131
+ ```
132
+
133
+ These work the same way as `redisCommand()` and `redisCommandArgv()`
134
+ (see [Construct synchronous commands](#construct-synchronous-commands)
135
+ above) but they have two extra parameters. The first is a pointer to
136
+ a optional callback function and the second is a pointer to your own
137
+ custom data, which will be passed to the callback when it
138
+ executes. Pass `NULL` for both of these pointers if you don't need
139
+ to use them.
140
+
141
+ The callback has the following signature:
142
+
143
+ ```c
144
+ void(redisAsyncContext *c, void *reply, void *privdata);
145
+ ```
146
+
147
+ The first parameter is the asynchronous connection context and
148
+ the second is a pointer to the reply object. Use a cast to
149
+ ` (redisReply *) ` to access the reply in the usual way (see
150
+ [ Handle command replies] ({{< relref "/develop/clients/hiredis/handle-replies" >}})
151
+ for a full description of ` redisReply ` ). The last parameter
152
+ is the custom data pointer that you supplied during the
153
+ ` redisAsyncCommand() ` call. This is passed to your function
154
+ without any modification.
155
+
156
+ The example below shows how you can use ` redisAsyncCommand() ` with
157
+ or without a reply callback:
158
+
159
+ ``` c
160
+ // The callback expects the key for the data in the `privdata`
161
+ // custom data parameter.
162
+ void getCallback (redisAsyncContext * c, void * r, void * privdata) {
163
+ redisReply * reply = r;
164
+ char * key = privdata;
165
+
166
+ if (reply == NULL) {
167
+ if (c->errstr) {
168
+ printf("errstr: %s\n", c->errstr);
169
+ }
170
+ return;
171
+ }
172
+
173
+ printf("Key: %s, value: %s\n", key, reply->str);
174
+
175
+ /* Disconnect after receiving the reply to GET */
176
+ redisAsyncDisconnect(c);
177
+ }
178
+ .
179
+ .
180
+ .
181
+
182
+ // Key and string value to pass to ` SET ` .
183
+ char * key = "testkey";
184
+ char * value = "testvalue";
185
+
186
+ // We aren't interested in the simple status reply for
187
+ // ` SET ` , so use NULL for the callback and custom data
188
+ // pointers.
189
+ redisAsyncCommand(c, NULL, NULL, "SET %s %s", key, value);
190
+
191
+ // The reply from ` GET ` is essential, so set a callback
192
+ // to retrieve it. Also, pass the key to the callback
193
+ // as the custom data.
194
+ redisAsyncCommand(c, getCallback, key, "GET %s", key);
195
+ ```
196
+
197
+ Note that you should normally disconnect asynchronously from a
198
+ callback when you have finished using the connection.
199
+ Use `redisAsyncDisconnect()` to disconnect gracefully, letting
200
+ pending commands execute and activate their callbacks.
201
+ Use `redisAsyncFree()` to disconnect immediately. If you do this then
202
+ any pending callbacks from commands that have already executed will be
203
+ called with a `NULL` reply pointer.
204
+
119
205
## Command replies
120
206
121
207
The information in the `redisReply` object has several formats,
0 commit comments