hiredis/libevent-example.c

31 lines
851 B
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/libevent.h>
2010-10-10 18:51:27 +00:00
#include <signal.h>
void getCallback(redisContext *c, redisReply *reply, void *privdata) {
printf("argv[%s]: %s\n", (const char*)privdata, reply->reply);
/* Disconnect after receiving the reply to GET */
redisDisconnect(c);
}
2010-10-19 14:48:19 +00:00
void errorCallback(const redisContext *c) {
printf("Error: %s\n", c->error);
}
int main (int argc, char **argv) {
signal(SIGPIPE, SIG_IGN);
struct event_base *base = event_base_new();
2010-10-19 18:13:10 +00:00
redisContext *c = libeventRedisConnect(base, errorCallback, "127.0.0.1", 6379);
if (c == NULL) return 1;
redisCommand(c, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
redisCommandWithCallback(c, getCallback, (char*)"end-1", "GET key");
event_base_dispatch(base);
redisFree(c);
return 0;
}