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