]> git.saurik.com Git - redis.git/blob - deps/hiredis/example-libev.c
199d706c9a95f299c4082ffec4fd7cdf5ea70617
[redis.git] / deps / hiredis / example-libev.c
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
18 void disconnectCallback(const redisAsyncContext *c, int status) {
19 if (status != REDIS_OK) {
20 printf("Error: %s\n", c->errstr);
21 }
22 }
23
24 int main (int argc, char **argv) {
25 signal(SIGPIPE, SIG_IGN);
26 struct ev_loop *loop = ev_default_loop(0);
27
28 redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
29 if (c->err) {
30 /* Let *c leak for now... */
31 printf("Error: %s\n", c->errstr);
32 return 1;
33 }
34
35 redisLibevAttach(c,loop);
36 redisAsyncSetDisconnectCallback(c,disconnectCallback);
37 redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
38 redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
39 ev_loop(loop, 0);
40 return 0;
41 }