1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 
6 #include <hiredis.h>
7 #include <async.h>
8 #include <adapters/libev.h>
9 
getCallback(redisAsyncContext * c,void * r,void * privdata)10 void getCallback(redisAsyncContext *c, void *r, void *privdata) {
11     redisReply *reply = r;
12     if (reply == NULL) return;
13     printf("argv[%s]: %s\n", (char*)privdata, reply->str);
14 
15     /* Disconnect after receiving the reply to GET */
16     redisAsyncDisconnect(c);
17 }
18 
connectCallback(const redisAsyncContext * c,int status)19 void connectCallback(const redisAsyncContext *c, int status) {
20     if (status != REDIS_OK) {
21         printf("Error: %s\n", c->errstr);
22         return;
23     }
24     printf("Connected...\n");
25 }
26 
disconnectCallback(const redisAsyncContext * c,int status)27 void disconnectCallback(const redisAsyncContext *c, int status) {
28     if (status != REDIS_OK) {
29         printf("Error: %s\n", c->errstr);
30         return;
31     }
32     printf("Disconnected...\n");
33 }
34 
main(int argc,char ** argv)35 int main (int argc, char **argv) {
36     signal(SIGPIPE, SIG_IGN);
37 
38     redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
39     if (c->err) {
40         /* Let *c leak for now... */
41         printf("Error: %s\n", c->errstr);
42         return 1;
43     }
44 
45     redisLibevAttach(EV_DEFAULT_ c);
46     redisAsyncSetConnectCallback(c,connectCallback);
47     redisAsyncSetDisconnectCallback(c,disconnectCallback);
48     redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
49     redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
50     ev_loop(EV_DEFAULT_ 0);
51     return 0;
52 }
53