Skip to content

Instantly share code, notes, and snippets.

@badboy
Created December 8, 2014 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badboy/823e9c97d249a65307cd to your computer and use it in GitHub Desktop.
Save badboy/823e9c97d249a65307cd to your computer and use it in GitHub Desktop.
#include "fmacros.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <assert.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include "adapters/libevent.h"
#include "hiredis.h"
#include "async.h"
struct event *timer_ev;
void getCallback(redisAsyncContext *c, void *r, void *privdata) {
redisReply *reply = r;
if (reply == NULL) return;
printf("argv[%s]: %s\n", (char*)privdata, reply->str);
evtimer_del(timer_ev);
/* Disconnect after receiving the reply to GET */
redisAsyncDisconnect(c);
}
redisAsyncContext *c;
void quit_all(int fd, short event, void *arg) {
(void) fd;
(void) event;
(void) arg;
redisAsyncDisconnect(c);
}
int main(void) {
const char *host = "::1";
int port = 6379;
struct event_base *base = event_base_new();
c = redisAsyncConnect(host, port);
if (c == NULL) {
printf("Error: c == NULL\n");
return 2;
}
if (c->err) {
printf("Error: %s\n", c->errstr);
return 1;
}
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
timer_ev = evtimer_new(base, quit_all, NULL);
evtimer_add(timer_ev, &tv);
redisLibeventAttach(c,base);
redisAsyncCommand(c, NULL, NULL, "SUBSCRIBE foo");
redisAsyncCommand(c, NULL, NULL, "UNSUBSCRIBE foo");
redisAsyncCommand(c, NULL, NULL, "SUBSCRIBE foo");
event_base_dispatch(base);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment