1、同步调用:
#include <stdio.h>
#include "hiredis/hiredis.h"
int main() {
redisContext *c = redisConnect("127.0.0.1", 6379);
if (c == NULL) {
printf("Can't allocate redis context\n");
return 1;
}
if (c->err) {
printf("Error: %s\n", c->errstr);
return 1;
}
printf("redisConnect success!\n");
char *key = "hello";
char *val = "redis";
redisReply *reply = redisCommand(c, "set %s %s", key, val);
if(reply->type == REDIS_REPLY_STATUS) {
printf("set %s %s\n", key, val);
}
freeReplyObject(reply);
reply = redisCommand(c, "GET %s", key);
if (reply->type == REDIS_REPLY_STRING) {
printf("get %s\n", reply->str);
}
freeReplyObject(reply);
redisFree(c);
return 0;
}
输出:
redisConnect success!
set hello redis
get redis
2、Pipelining:
#include <stdio.h>
#include "hiredis/hiredis.h"
int main() {
redisContext *c = redisConnect("127.0.0.1", 6379);
if (c == NULL) {
printf("Can't allocate redis context\n");
return 1;
}
if (c->err) {
printf("Error: %s\n", c->errstr);
return 1;
}
printf("redisConnect success!\n");
redisReply *reply = NULL;
redisAppendCommand(c,"set hello redis");
redisAppendCommand(c,"GET hello");
redisGetReply(c,&reply);
if(reply->type == REDIS_REPLY_STATUS) {
printf("set hello redis\n");
}
freeReplyObject(reply);
redisGetReply(c,&reply);
if (reply->type == REDIS_REPLY_STRING) {
printf("get %s\n", reply->str);
}
freeReplyObject(reply);
redisFree(c);
return 0;
}
输出:
redisConnect success!
set hello redis
get redis
3、异步调用:
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include "event2/event.h"
#include "hiredis/adapters/libevent.h"
void getCallback(redisAsyncContext *c, void *r, void *privdata) {
redisReply *reply = r;
if (reply == NULL) return;
printf("argv[%s]: %s\n", (char*)privdata, reply->str);
/* Disconnect after receiving the reply to GET */
redisAsyncDisconnect(c);
}
void connectCallback(const redisAsyncContext *c, int status) {
if (status != REDIS_OK) {
printf("Error: %s\n", c->errstr);
return;
}
printf("Connected...\n");
}
void disconnectCallback(const redisAsyncContext *c, int status) {
if (status != REDIS_OK) {
printf("Error: %s\n", c->errstr);
return;
}
printf("Disconnected...\n");
}
int main (int argc, char **argv) {
signal(SIGPIPE, SIG_IGN);
struct event_base *base = event_base_new();
redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
if (c == NULL) {
printf("Can't allocate redis context\n");
return 1;
}
if (c->err) {
printf("Error: %s\n", c->errstr);
return 1;
}
printf("redisAsyncConnect success!\n");
redisLibeventAttach(c,base);
redisAsyncSetConnectCallback(c,connectCallback);
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");
event_base_dispatch(base);
return 0;
}
输出:
$ ./hiredis_test 2019
redisAsyncConnect success!
Connected...
argv[end-1]: 2019
Disconnected...
参考文献:
hiredis源代码的README文件:
https://github.com/redis/hiredis/README.md
hiredis异步调用:
https://github.com/redis/hiredis/examples/example-libevent.c