(注:整个demo是在release模式下的)
1.链接redis之前,先解压github下载的整个zip。
redis-3.0->msvs->RedisServer
编译hiredis和Win32_Interlop,产生这两个.lib文件
(可能产生的错误:平台工具集不兼容,解决:
Project->Properties->General->Platform Toolset->vs2015 (v140)
高版本可以兼容低版本,低版本不能兼容高的。
)
(因为后面产生了报错,是关于运行时库mt和md的,根据实际需要去调整,又重新在release模式下编译生成lib)
2.头文件需要包含Win32_Interlop和hiredis全部的头文件。
hiredis的头文件在deps里,其中fmarcos.h要用src里的fmarcos.h替换
3.调整包含头文件和lib文件的目录方法同链接mysql,记得要加上hiredis和Win32_Interlop两个lib。
4.platform也要改成x64。
5.Project->Properties->C/C++->preprocessor defination:NO_QFORKIMPL,WIN32_IOCP
6.Project->Properties->C/C++->Code Generation->Runtime Library->MTD
7.把所有的头文件添加到工程中,因为有一个头文件,好像是win32那边的,会报错,因为路径问题。把他原来指定的相对路径换成现在目录下的路径就可以消除报错。
// redis_demo.cpp :set/get/strlen/lpush/lpop命令
//可以配合redis命令手册使用,就会知道具体的指令返回是什么类型的数据
#pragma comment (lib,"hiredis.lib")
#pragma comment(lib,"Win32_Interop.lib")
#include "stdafx.h"
#include "hiredis.h"
#include <iostream>
void test() {
redisContext* c = redisConnect((char*)"192.168.137.1", 6379);
if (c->err) {
redisFree(c);
return;
}
const char* command1 = "set stest1 value9";
redisReply* r = (redisReply*)redisCommand(c, command1);
if (NULL == r) {
redisFree(c);
return;
}
if (!(r->type == REDIS_REPLY_STATUS && (strcmp(r->str,"OK")) == 0 || strcmp(r->str,"ok") ==0 )) {
std::cout << "failed to execute command" << command1 << std::endl;
freeReplyObject(r);
redisFree(c);
return;
}
freeReplyObject(r);
std::cout << "succeed to execute command " << command1 << std::endl;
const char* command2 = "strlen stest1";
r = (redisReply*)redisCommand(c,command2);
if (r->type != REDIS_REPLY_INTEGER) {
std::cout << "failed to execute command " << command2 << std::endl;
freeReplyObject(r);
redisFree(c);
return;
}
int length = r->integer;
freeReplyObject(r);
std::cout << "the length of stest1 is " << length << std::endl;
std::cout << "succeed to execute command " << command2 << std::endl;
const char* command3 = "get stest1";
r = (redisReply*)redisCommand(c, command3);
if (r->type != REDIS_REPLY_STRING) {
std::cout << "failed to execute command" << command3 << std::endl;
freeReplyObject(r);
redisFree(c);
return;
}
std::cout << "the value of stest1 is " << r->str << std::endl;
freeReplyObject(r);
std::cout << "succeed to execute command " << command3 << std::endl;
const char* command4 = "lpush list1 hello ";
r = (redisReply*)redisCommand(c,command4);
if (r->type != REDIS_REPLY_INTEGER) {
std::cout << "failed to execute command " << command4 << std::endl;
freeReplyObject(r);
redisFree(c);
return;
}
length = r->integer;
freeReplyObject(r);
std::cout << "the length of list1 is " << length << std::endl;
std::cout << "succeed to execute command " << command4 << std::endl;
const char* command6 = "lpop list1 ";
r = (redisReply*)redisCommand(c, command6);
if (r->type != REDIS_REPLY_STRING) {
std::cout << "failed to execute command " << command6 << std::endl;
freeReplyObject(r);
redisFree(c);
return;
}
std::cout << "the value of lpop list1 is " << r->str << std::endl;
freeReplyObject(r);
std::cout << "succeed to execute command " << command6 << std::endl;
redisFree(c);
//char i;
//std::cin >> i;
}
int main()
{
test();
return 0;
}
错误处理参考(http://blog.sina.com.cn/s/blog_47379bd80102vbtb.html)
(http://www.th7.cn/Program/cp/201411/321709.shtml)
代码参考
(http://www.2cto.com/database/201504/387552.html)
“`