]> git.saurik.com Git - redis.git/blob - deps/hiredis/example-libevent.c
Re-use AOF buffer when it is small enough
[redis.git] / deps / hiredis / example-libevent.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/libevent.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 connectCallback(const redisAsyncContext *c) {
19 ((void)c);
20 printf("connected...\n");
21 }
22
23 void disconnectCallback(const redisAsyncContext *c, int status) {
24 if (status != REDIS_OK) {
25 printf("Error: %s\n", c->errstr);
26 }
27 printf("disconnected...\n");
28 }
29
30 int main (int argc, char **argv) {
31 signal(SIGPIPE, SIG_IGN);
32 struct event_base *base = event_base_new();
33
34 redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
35 if (c->err) {
36 /* Let *c leak for now... */
37 printf("Error: %s\n", c->errstr);
38 return 1;
39 }
40
41 redisLibeventAttach(c,base);
42 redisAsyncSetConnectCallback(c,connectCallback);
43 redisAsyncSetDisconnectCallback(c,disconnectCallback);
44 redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
45 redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
46 event_base_dispatch(base);
47 return 0;
48 }