1.什么是hiredis
Hiredis 是一个 C 语言编写的 Redis 客户端库,用于与 Redis 数据库进行交互。它提供了一个简洁而高效的接口,使开发人员可以方便地在自己的 C/C++ 项目中使用 Redis。
Hiredis 是一个开源项目,可从其官方 GitHub 仓库获取源代码,并在符合 BSD 许可证的条件下使用和分发。它被广泛应用于各种 C/C++ 项目中,特别是那些需要与 Redis 数据库进行快速、可靠和高性能交互的应用程序。
2.安装hiredis
在centos
中可以使用yum
直接安装:
yum install hiredis-devel
或者直接从源码安装:
1 2 3 4
| git clone https://github.com/redis/hiredis.git cd hiredis/ make make install
|
3.使用hiredis
使用同步的方式连接redis,只需要使用以下几个函数:
1 2 3
| redisContext *redisConnect(const char *ip, int port); void *redisCommand(redisContext *c, const char *format, ...); void freeReplyObject(void *reply);
|
其中,发送指令的函数在使用上与printf
类似,支持不定参数:
1 2 3
| reply = redisCommand(context, "SET foo bar") reply = redisCommand(context, "SET foo %s", value) reply = redisCommand(context, "SET key:%s %s", myid, value)
|
同时也支持二进制的参数:
reply = redisCommand(context, "SET foo %b", value, (size_t) valuelen);
用%b
表示二进制,然后传入二进制value
及其长度valuelen
可以封装成一个C++类:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #include <hiredis/hiredis.h> #include <iostream> #include <string> using namespace std;
class Redis { public: Redis(const char * host, int port = 6379){ c = redisConnect(host, port); if (c == NULL){ cout << "cannot allocate redis context" << endl; return; }
if (c->err){ cout << c->errstr << endl; c = NULL; return; } }
~Redis(){ if (c){ redisFree(c); } }
bool isConnect() { return c != NULL; }
bool Set(const char * key, const char * value) { redisReply *reply = (redisReply*) redisCommand(c, "set %s %s", key, value); if (reply == NULL){ return false; } bool success = false; if (reply->type == REDIS_REPLY_STATUS){ cout << reply->str << endl; success = true; } else if (reply->type == REDIS_REPLY_ERROR) { cout << reply->str << endl; }
freeReplyObject(reply); return success; }
string Get(const char* key) { redisReply *reply = (redisReply*) redisCommand(c, "get %s", key); string value; if (reply == NULL){ return value; } if (reply->type == REDIS_REPLY_STRING){ value = reply->str; }else if (reply->type == REDIS_REPLY_ERROR){ cout << reply->str << endl; } freeReplyObject(reply); return value; }
bool HSet(const char * key, const char * field, const char * value) { redisReply *reply = (redisReply*) redisCommand(c, "hset %s %s %s", key, field, value); if (reply == NULL){ return false; } bool success = false; if (reply->type == REDIS_REPLY_STATUS){ cout << reply->str << endl; success = true; } else if (reply->type == REDIS_REPLY_ERROR) { cout << reply->str << endl; } freeReplyObject(reply); return success; }
string HGet(const char* key, const char * field) { redisReply *reply = (redisReply*) redisCommand(c, "hget %s %s", key, field); string value; if (reply == NULL){ return value; } if (reply->type == REDIS_REPLY_STRING){ value = reply->str; }else if (reply->type == REDIS_REPLY_ERROR){ cout << reply->str << endl; } freeReplyObject(reply); return value; }
private: redisContext *c;
};
int main(){ Redis r("127.0.0.1", 6379); if (!r.isConnect()){ return 0; }
r.Set("id", "100"); string value = r.Get("id"); cout << value << endl;
r.HSet("map", "1", "200"); cout << r.HGet("map", "1") << endl;
return 0; }
|
4.hiredis的异步模式
使用hiredis
的同步模式时,每一个请求都需要等待回应之后,才能进行下一个请求,时间消耗在IO等待上。为了提高效率,可以使用异步模式,同时发送多个请求,然后回调处理。
使用hiredis
的异步模式,需要绑定一个事件适配器,常用的有libevent
libuv
等。
下面以libevent
为例:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <iostream> #include <signal.h> #include <hiredis/async.h> #include <hiredis/adapters/libevent.h>
struct event_base *eventBase = nullptr;
void signalHandler(int sig, short events, void *arg) { printf("Received signal %d, exiting...\n", sig);
event_base_loopbreak(eventBase); }
void commandCallback(redisAsyncContext* context, void* reply, void* data) { redisReply* r = reinterpret_cast<redisReply*>(reply); if (r == nullptr) { std::cout << "Command error: " << context->errstr << std::endl; return; }
if (r->type == REDIS_REPLY_ERROR) { std::cout << "Command error: " << r->str << std::endl; return; }
std::cout << r->str << std::endl;
}
void connectCallback(const redisAsyncContext* context, int status) { if (status == REDIS_OK) { std::cout << "Connected to Redis" << std::endl;
redisAsyncContext* asyncContext = const_cast<redisAsyncContext*>(context); redisAsyncCommand(asyncContext, commandCallback, nullptr, "SET mykey Hello"); redisAsyncCommand(asyncContext, commandCallback, nullptr, "GET mykey"); } else { std::cout << "Connection error" << std::endl; } }
int main() { eventBase = event_base_new(); redisAsyncContext* context = redisAsyncConnect("127.0.0.1", 6379);
if (context->err) { std::cout << "Connection error: " << context->errstr << std::endl; return 1; }
redisLibeventAttach(context, eventBase); redisAsyncSetConnectCallback(context, connectCallback);
struct event * signalEvent = evsignal_new(eventBase, SIGINT, signalHandler, nullptr);; evsignal_add(signalEvent, nullptr);
std::cout << "开始事件循环..." << std::endl; event_base_dispatch(eventBase); std::cout << "事件循环结束..." << std::endl;
event_free(signalEvent); event_base_free(eventBase); redisAsyncDisconnect(context);
return 0; }
|
参考资料