summaryrefslogtreecommitdiff
path: root/libevent-example.c
blob: 9c8285064ae4f7a10e0e72fa51b712204fcb6636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/libevent.h>
#include <signal.h>

void getCallback(redisContext *c, redisReply *reply, void *privdata) {
    if (reply == NULL) return; /* Error */
    printf("argv[%s]: %s\n", (const char*)privdata, reply->reply);

    /* Disconnect after receiving the reply to GET */
    redisDisconnect(c);
}

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();

    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;
}