hiredis的同步模式和异步模式

发布时间 2023-06-22 16:42:45作者: 看热闹的咸鱼

1.什么是hiredis

Hiredis 是一个 C 语言编写的 Redis 客户端库,用于与 Redis 数据库进行交互。它提供了一个简洁而高效的接口,使开发人员可以方便地在自己的 C/C++ 项目中使用 Redis。
Hiredis 是一个开源项目,可从其官方 GitHub 仓库获取源代码,并在符合 BSD 许可证的条件下使用和分发。它被广泛应用于各种 C/C++ 项目中,特别是那些需要与 Redis 数据库进行快速、可靠和高性能交互的应用程序。

2.安装hiredis

centos中可以使用yum直接安装:
yum install hiredis-devel

或者直接从源码安装:

git clone https://github.com/redis/hiredis.git
cd hiredis/
make
make install

3.使用hiredis

使用同步的方式连接redis,只需要使用以下几个函数:

redisContext *redisConnect(const char *ip, int port);
void *redisCommand(redisContext *c, const char *format, ...);
void freeReplyObject(void *reply);

其中,发送指令的函数在使用上与printf类似,支持不定参数:

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++类:

// g++ hiredis.cpp -lhiredis
#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为例:

#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;

    // 这里不需要释放 reply 对象,hiredis的异步机制会自动释放
}

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;
}

参考资料