下面是 hiredis 异步调用使用的一个例子(算是对于官方简单例子的一个补充,可能长得更像实际业务中使用的样子),一般使用场景是我们监听一个端口,当有请求来的时候,就执行回调,而回调里面可能需要读写 Redis。(涉及 IO 的操作都需要异步处理!)
#include <hiredis/hiredis.h>
#include <hiredis/adapters/libevent.h>
#include <sys/socket.h>
void do_accept(evutil_socket_t listener, short event, void *arg) {
redisAsyncContext *ac = (redisAsyncContext *)arg;
evutil_socket_t fd;
struct sockaddr_in sin;
socklen_t slen = sizeof(sin);
fd = accept(listener, (struct sockaddr *)&sin, &slen);
if (fd < 0) {
perror("[ERROR] accept error");
return;
}
redisAsyncCommand(ac, NULL, NULL, "SET key test");
}
int main() {
signal(SIGPIPE, SIG_IGN);
struct event_base *base = event_base_new();
/* 创建套接字 */
int listenfd;
struct sockaddr_in servaddr;
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("[ERROR] socket error");
exit(-1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(17777);
bind(listenfd, (const struct sockaddr*) &servaddr, sizeof(servaddr));
if (listen(listenfd, 1024) < 0) {
printf("[ERROR] listen error");
exit(-1);
}
redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
if (c->err) {
/* Let *c leak for now... */
printf("Error: %s\n", c->errstr);
return 1;
}
redisLibeventAttach(c,base);
c->data = (void *)base;
struct event *listen_event;
listen_event = event_new(base, listenfd, EV_READ|EV_PERSIST, do_accept, (void *)c);
event_add(listen_event, NULL);
event_base_dispatch(base);
return 0;
}