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/libevent.h> 9 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 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 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 35 int main (int argc, char **argv) { 36 signal(SIGPIPE, SIG_IGN); 37 struct event_base *base = event_base_new(); 38 39 redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); 40 if (c->err) { 41 /* Let *c leak for now... */ 42 printf("Error: %s\n", c->errstr); 43 return 1; 44 } 45 46 redisLibeventAttach(c,base); 47 redisAsyncSetConnectCallback(c,connectCallback); 48 redisAsyncSetDisconnectCallback(c,disconnectCallback); 49 redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1])); 50 redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key"); 51 event_base_dispatch(base); 52 return 0; 53 } 54