2010-09-25 20:34:22 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-10-10 18:51:27 +00:00
|
|
|
#include <signal.h>
|
2013-04-19 20:39:26 +00:00
|
|
|
|
|
|
|
#include <hiredis.h>
|
|
|
|
#include <async.h>
|
|
|
|
#include <adapters/libevent.h>
|
2010-09-25 20:34:22 +00:00
|
|
|
|
2010-11-01 12:21:26 +00:00
|
|
|
void getCallback(redisAsyncContext *c, void *r, void *privdata) {
|
|
|
|
redisReply *reply = r;
|
2018-12-06 09:55:47 +00:00
|
|
|
if (reply == NULL) {
|
|
|
|
if (c->errstr) {
|
|
|
|
printf("errstr: %s\n", c->errstr);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2010-11-01 09:17:28 +00:00
|
|
|
printf("argv[%s]: %s\n", (char*)privdata, reply->str);
|
2010-09-25 20:34:22 +00:00
|
|
|
|
2010-10-07 15:48:03 +00:00
|
|
|
/* Disconnect after receiving the reply to GET */
|
2010-11-01 09:17:28 +00:00
|
|
|
redisAsyncDisconnect(c);
|
2010-09-25 20:34:22 +00:00
|
|
|
}
|
|
|
|
|
2011-06-27 21:55:25 +00:00
|
|
|
void connectCallback(const redisAsyncContext *c, int status) {
|
|
|
|
if (status != REDIS_OK) {
|
|
|
|
printf("Error: %s\n", c->errstr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("Connected...\n");
|
2010-12-07 09:22:30 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
void disconnectCallback(const redisAsyncContext *c, int status) {
|
|
|
|
if (status != REDIS_OK) {
|
2010-11-02 16:37:22 +00:00
|
|
|
printf("Error: %s\n", c->errstr);
|
2011-06-27 21:55:25 +00:00
|
|
|
return;
|
2010-11-01 09:17:28 +00:00
|
|
|
}
|
2011-06-27 21:55:25 +00:00
|
|
|
printf("Disconnected...\n");
|
2010-09-25 20:34:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char **argv) {
|
2020-07-10 19:53:52 +00:00
|
|
|
#ifndef _WIN32
|
2010-09-25 20:34:22 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2020-07-10 19:53:52 +00:00
|
|
|
#endif
|
|
|
|
|
2010-10-11 16:26:38 +00:00
|
|
|
struct event_base *base = event_base_new();
|
2018-12-06 09:55:47 +00:00
|
|
|
redisOptions options = {0};
|
|
|
|
REDIS_OPTIONS_SET_TCP(&options, "127.0.0.1", 6379);
|
|
|
|
struct timeval tv = {0};
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
options.timeout = &tv;
|
|
|
|
|
2010-09-25 20:34:22 +00:00
|
|
|
|
2018-12-06 09:55:47 +00:00
|
|
|
redisAsyncContext *c = redisAsyncConnectWithOptions(&options);
|
2010-11-02 16:37:22 +00:00
|
|
|
if (c->err) {
|
2010-11-01 09:17:28 +00:00
|
|
|
/* Let *c leak for now... */
|
2010-11-02 16:37:22 +00:00
|
|
|
printf("Error: %s\n", c->errstr);
|
2010-11-01 09:17:28 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2010-09-25 20:34:22 +00:00
|
|
|
|
2010-11-01 09:17:28 +00:00
|
|
|
redisLibeventAttach(c,base);
|
2010-12-07 09:22:30 +00:00
|
|
|
redisAsyncSetConnectCallback(c,connectCallback);
|
2010-11-01 09:17:28 +00:00
|
|
|
redisAsyncSetDisconnectCallback(c,disconnectCallback);
|
|
|
|
redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc-1], strlen(argv[argc-1]));
|
|
|
|
redisAsyncCommand(c, getCallback, (char*)"end-1", "GET key");
|
2010-10-11 16:26:38 +00:00
|
|
|
event_base_dispatch(base);
|
2010-09-25 20:34:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|