一、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