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/ae.h>
9 
10 /* Put event loop in the global scope, so it can be explicitly stopped */
11 static aeEventLoop *loop;
12 
getCallback(redisAsyncContext * c,void * r,void * privdata)13 void getCallback(redisAsyncContext *c, void *r, void *privdata) {
14     redisReply *reply = r;
15     if (reply == NULL) return;
16     printf("argv[%s]: %s\n", (char*)privdata, reply->str);
17 
18     /* Disconnect after receiving the reply to GET */
19     redisAsyncDisconnect(c);
20 }
21 
connectCallback(const redisAsyncContext * c,int status)22 void connectCallback(const redisAsyncContext *c, int status) {
23     if (status != REDIS_OK) {
24         printf("Error: %s\n", c->errstr);
25         aeStop(loop);
26         return;
27     }
28 
29     printf("Connected...\n");
30 }
31 
disconnectCallback(const redisAsyncContext * c,int status)32 void disconnectCallback(const redisAsyncContext *c, int status) {
33     if (status != REDIS_OK) {
34         printf("Error: %s\n", c->errstr);
35         aeStop(loop);
36         return;
37     }
38 
39     printf("Disconnected...\n");
40     aeStop(loop);
41 }
42 
main(int argc,char ** argv)43 int main (int argc, char **argv) {
44     signal(SIGPIPE, SIG_IGN);
45 
46     redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
47     if (c->err) {
48         /* Let *c leak for now... */
49         printf("Error: %s\n", c->errstr);
50         return 1;
51     }
52 
53     loop = aeCreateEventLoop(64);
54     redisAeAttach(loop, c);
55     redisAsyncSetConnectCallback(c,connectCallback);
56     redisAsyncSetDisconnectCallback(c,disconnectCallback);
57     redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
58     redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
59     aeMain(loop);
60     return 0;
61 }
62 
63