redis应用

C++调用Redis库实战指南
本文详细介绍了如何在C++中使用hiredis库连接、操作Redis,并提供了连接Redis、验证密码、执行命令及处理返回结果的示例代码。同时讲解了Linux环境下搭建Redis服务端的步骤,包括下载、编译、启动服务以及使用redis-cli测试连接。

一、C++中调用redis库

1、下载编译hiredis静态库

源码地址https://github.com/redis/hiredis

下载后解压,执行make

2、编写代码

头文件整理:将hiredis中的adapters文件夹和所有.h文件放到自己代码的头文件目录中;

库文件使用libhiredis.a,放到自己代码的库文件目录中;

2.1  连接redis


redisContext *pRedisContext;
u32         port;
std::string strRedisAddr;
std::string strRedisPassword;
BOOL32 ConnectRedis()
{
    struct timeval timeout = { 1, 500000 }; // 1.5 seconds
    //连接redis服务器
    pRedisContext = redisConnectWithTimeout(strRedisAddr.c_str(), port, timeout);
    if (NULL == pRedisContext || 0 != pRedisContext->err)
    {
        if (NULL != pRedisContext)
        {
            printf("连接Redis失败, 错误码[%u]\n", pRedisContext->err);
            redisFree(pRedisContext);
            pRedisContext = NULL;
        }
        else
        {
            printf("连接Redis失败, can't allocate redis context\n");
        }

        return FALSE;
    }

    //校验密码
    redisReply *reply= (redisReply *)redisCommand(pRedisContext, "AUTH %s", strRedisPassword.c_str());
    if (reply->type == REDIS_REPLY_ERROR) {

        /* Authentication failed */
        freeReplyObject(reply);
        redisFree(pRedisContext);
        printf( "Authentication failed : %s\n", reply->str);
        return FALSE;
    }

    freeReplyObject(reply);

    printf("连接Redis成功\n");
    return TRUE;
}


2.2 与redis服务器断开

void DisConnectRedis()
{
    if (pRedisContext)
    {
        redisFree(pRedisContext);
        pRedisContext = NULL;
    }
}


2.3 执行命令并处理返回结果,所有命令都可以调用redisCommand执行

unsigned int GetValue(const std::string &strCmd, std::vector<std::string> &vecList)
{
      unsigned int errorCode = 0;

    redisReply *pRedisReply = NULL;
    pRedisReply = (redisReply *)redisCommand(pRedisContext, strCmd.c_str());
    if (pRedisReply)
    {
        if (REDIS_REPLY_ERROR == pRedisReply->type)
        {
            printf( "执行命令失败, 错误信息[%s]\n", pRedisReply->str);
            errorCode = REDIS_ERR_RESULT_FAIL;
        }
        else
        {
            printf( "执行命令成功 type[%d] integer[%d] len[%u] str[%s] elementnum[%d]\n",
                                                    pRedisReply->type, pRedisReply->integer,
                                                    pRedisReply->len, pRedisReply->str, pRedisReply->elements);

            if (REDIS_REPLY_NIL == pRedisReply->type)
            {
                printf( "结果返回为空\n");
                errorCode = 1;
            }
            else if (REDIS_REPLY_STRING == pRedisReply->type)
            {
                if (NULL != pRedisReply->str)
                {
                    vecList.push_back(std::string(pRedisReply->str));
                }
                else
                {
                    vecList.push_back("");
                }
            }
            else if (REDIS_REPLY_ARRAY == pRedisReply->type)
            {
                printf( "结果是列表----------------------------------------------------\n");
                for (unsigned int  dwIndex = 0; dwIndex < pRedisReply->elements; dwIndex++)
                {
                    redisReply *pValueElement = pRedisReply->element[dwIndex];

                    if (REDIS_REPLY_STRING == pValueElement->type)
                    {
                        if (NULL != pValueElement && NULL != pValueElement->str)
                        {
                            vecList.push_back(std::string(pValueElement->str));
                        }
                        else
                        {
                            vecList.push_back("");
                        }
                    }
                    else if (REDIS_REPLY_INTEGER == pValueElement->type)
                    {
                        char achValue[32] = {0};
                        sprintf(achValue, "%u", pValueElement->integer);
                        vecList.push_back(std::string(achValue));
                    }
                    else if (REDIS_REPLY_NIL == pValueElement->type)
                    {
                        vecList.push_back("");
                    }
                }
            }
            else if (REDIS_REPLY_INTEGER == pRedisReply->type)
            {
                char achValue[32] = {0};
                sprintf(achValue, "%u", pRedisReply->integer);
                vecList.push_back(std::string(achValue));
            }
            else
            {
                printf( "返回结果类型不正确\n");
                errorCode = 1;
            }
        }

        freeReplyObject(pRedisReply);
    }
    else
    {
        errorCode = 1;
        printf("执行命令失败 调用 redisCommandArgv 失败\n");
    }

    return errorCode;
}


二、Linux下搭建redis服务端

1、下载redis

redis官网:https://redis.io/download

下载完成后,解压编译会生成redis-server,启动程序 ./redis-server  ./conf/redis.conf

测试启动redis客户端并连接redis服务器

./redis-cli -h IP地址 -p端口 -a 密码   例子./redis-cli -h 127.0.0.1 -p 6379 -a admin888

三、知识延伸:

redis的使用教程:https://www.runoob.com/redis/redis-tutorial.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值