c++调用hiredis库操作redis讲解

   首先下载编译hiredis,访问https://github.com/redis/hiredis下载hiredis库源码,我的编译环境为ubuntu14,解压hiredis源码后,终端下进入源码目录,然后输入make命令编译即可,编译后将分别得到静态及动态库文件:libhiredis.a / libhiredis.so,然后结合头文件就可以在工程中使用redis了。项目中我用是静态库libhiredis.a及头文件:hiredis.h,read.h,sds.h,使用静态库还是动态库,因人而异。
1. 下面是根据服务器ip、端口及密码,连接redis代码示例:
void redisConnect(string serverAddr, uint16_t port, string password)
{
    LOG_INFO << "开始连接redis服务器..." << serverAddr << ":" << port;
 
    // 连接
    timeval timeout = { 3, 500000 };
    redis_ctx_ = redisConnectWithTimeout(serverAddr.c_str(), port, timeout);
    if (redis_ctx_ == NULL || redis_ctx_->err)
    {
        if (redis_ctx_ != NULL)
        {
            LOG_INFO << "连接异常: " << redis_ctx_->errstr;
            redisFree(redis_ctx_);
            redis_ctx_ = NULL;
        }
        else
        {
            LOG_INFO << "连接异常: redis context初始化出错";
        }
 
        // todo 延迟30秒重新连接redis服务器
        return;
    }
    else
    {
        LOG_INFO << "连接redis服务器成功..." << serverAddr << ":" << port;
    }
 
    // 验证
    redis_reply_ = (redisReply *)redisCommand(redis_ctx_, "auth %s", password.c_str());
    if (redis_reply_->type == REDIS_REPLY_ERROR)
    {
        LOG_INFO << "Authentication failure";
    }
    else
    {
        LOG_INFO << "Authentication success";
    }
    freeReplyObject(redis_reply_);
}
        结合输出日志一起看,代码也容易懂。上面的延迟30秒重连机制需要自己设计。
2. 分发数据至redis服务器(redis发布订阅模式):
  主要调用redisCommand(),按照指定格式填写命令参数,就可以执行各种redis命令。这里不再一一列举。不过hiredis在实际应用中性能如何,还有待验证。
void distributeRedisMessage(const string &content, const string &topic)
{
    if (redis_ctx_ == NULL)
    {
        return;
    }
 
    redis_reply_ = (redisReply *)redisCommand(redis_ctx_, "publish %s %s", topic.c_str(), content.c_str());
    if (redis_reply_ != NULL)
    {
        if (redis_reply_->type == REDIS_REPLY_ERROR)
        {
            LOG_INFO << "命令发送失败: " << redis_reply_->type << " " << redis_reply_->str;
        }
        freeReplyObject(redis_reply_);
    }
    else
    {
        LOG_INFO << "与redis服务器连接异常, 命令发送失败: " << redis_ctx_->err << " " << redis_ctx_->errstr;
        redisFree(redis_ctx_);
        redis_ctx_ = NULL;
 
        // todo 延迟30秒重新连接redis服务器
    }
}
    当然,上面都是封装好的方法,还要记得声明redisContext *redis_ctx_及redisReply *redis_reply_,并记得初始化。
    工作中也会写些java服务端程序,jedis我也在用,这里顺便提一下前几天遇到的一个jedis报错,很不好查。错误提示如下:
Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool.查来查去,最后发现是配置文件问题。
主要包括如下四个方法
1. redisContext* redisConnect(const char *ip, int port)
   该函数用来连接redis数据库, 两个参数分别是redis数据库的ip和端口,端口号一般为6379。类似的还提供了一个函数,供连接超时限定,即
   redisContext* redisConnectWithTimeout(const char *ip, int port, timeval tv)。
2. void *redisCommand(redisContext *c, const char *format...)
   该函数用于执行redis数据库中的命令,第一个参数为连接数据库返回的redisContext,剩下的参数为变参,如同C语言中的prinf()函数。
   此函数的返回值为void*,但是一般会强制转换为redisReply类型,以便做进一步的处理。

3. void freeReplyObject(void *reply)
   释放redisCommand执行后返回的的redisReply所占用的内存。

4. void redisFree(redisContext *c)
   释放redisConnect()所产生的连接。
   
   c++使用hiredis库操作redis讲解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值