C++ 中使用redis
安装好redis后,进入deps目录,执行make intall则会将hiredis.h等文件 copy到 /usr/local/include/hiredis/
会将 lib文件放入/usr/local/lib/ 中。
在C++中时候用hiredis.h时,只需要
#include <hiredis/hiredis.h> 这样引入即可。
不多说了,上代码 testRedis.cpp ,出处 http://blog.csdn.net/kingqizhou/article/details/8104693:
#include <stdio.h>
#include <stdlib.h>
#include <hiredis/hiredis.h>
#include <string>
#include <iostream>
#include <string.h>
void doTest(std::string ip,unsigned intport)
{
//redis默认监听端口为6387 可以再配置文件中修改
redisContext* redis = redisConnect(ip.c_str(), port);
if ( NULL == redis || redis->err)
{ // redis为NULL与redis->err是两种不同的错误,若redis->err为true,可使用redis->errstr查看错误信息
redisFree(redis);
std::cout << "Connect to redisserver failed" << std::endl;
return ;
}
std::cout << "Connect to redisserver success" << std::endl;
const char* command1 = "set stest1 value1";
redisReply* reply = (redisReply*)redisCommand(redis, command1); // 执行命令,结果强转成redisReply*类型
if( NULL == reply)
{
printf("Execut command1 failure\n");
redisFree(redis); // 命令执行失败,释放内存
return;
}
if( !(reply->type == REDIS_REPLY_STATUS && strcasecmp(reply->str,"OK")==0))
{ // 判断命令执行的返回值
printf("Failed to execute command[%s]\n",command1);
freeReplyObject(reply);
redisFree(redis);
return;
}
freeReplyObject(reply);
printf("Succeed to execute command[%s]\n", command1);
// 一切正常,则对返回值进行处理v
const char* command2 = "strlen stest1";
reply = (redisReply*)redisCommand(redis, command2); // 执行命令,结果强转成redisReply*类型
if ( reply->type != REDIS_REPLY_INTEGER)
{ // 判断命令执行的返回值
printf("Failed to execute command[%s]\n",command2);
freeReplyObject(reply);
redisFree(redis);
return;
}
freeReplyObject(reply);
std::cout << "result of Command strlen test1: " << reply->integer << std::endl;
const char* command3 = "get stest1";
redisReply*r = (redisReply*)redisCommand(redis, command3);
if ( r->type != REDIS_REPLY_STRING)
{
printf("Failed to execute command[%s]\n",command3);
freeReplyObject(r);
redisFree(redis);
return;
}
printf("The value of 'stest1' is %s\n", r->str);
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command3);
const char* command4 = "get stest2";
r = (redisReply*)redisCommand(redis, command4);
if ( r->type != REDIS_REPLY_NIL)
{
printf("Failed to execute command[%s]\n",command4);
freeReplyObject(r);
redisFree(redis);
return;
}
printf("The value of 'stest2' is %s\n", r->str);
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command4);
redisFree(redis);
}
int main()
{
std::string ip = "127.0.0.1";
unsigned int prot = 6379;
doTest(ip,prot);
}
编译 g++ -o testRedis testRedis.cpp
报错:
testRedis.cpp:(.text+0x1d): undefined reference to `redisConnect'
testRedis.cpp:(.text+0x3a): undefined reference to `redisFree'
testRedis.cpp:(.text+0xa0): undefined reference to `redisCommand'
testRedis.cpp:(.text+0xc0): undefined reference to `redisFree'
testRedis.cpp:(.text+0x104): undefined reference to `freeReplyObject'
testRedis.cpp:(.text+0x10f): undefined reference to `redisFree'
testRedis.cpp:(.text+0x11c): undefined reference to `freeReplyObject'
于是 使用了 g++ -lhiredis -o testRedis testRedis.cpp。
编译通过。
运行./testRedis
报错
./testRedis: error while loading shared libraries: libhiredis.so.0.10: cannot open shared object file: No such file or directory
显然是没有找到 libhiredis.so.0.10 这文件,但是我们可以看到 libhiredis.so.0.10 是在/usr/local/lib/ 中的。
解决方法 参考 http://blog.iyunv.com/uid-26212859-id-3256667.html
页:
[1]