1、Redis
2、下载
去github上下载微软版的redis源码,版本为3.2
https://github.com/MicrosoftArchive/redis
3、编译
只需要编译hiredis和Win32_Interop两个项目,这个编译没什么问题。只不过需要注意WIN32和X64版本,
生成hiredis.lib和Win32_Interop.lib,放到一个目录里(我的是在D:\Redis\lib),同时把之前的源文件代码里的deps文件夹和src文件夹拷到另外一个目录(我的是在D:\Redis\include)。
4、将lib引入到自己的控制台工程
建立控制台工程,然后把示例代码拷贝进去,以下是我的代码:
发布者模式
// ConsoleApplication11.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <hiredis.h>
#define NO_QFORKIMPL
extern "C"
{
#include <win32fixes.h>
}
#include <ICRSINT.H>
#include <windows.h>
#include "win32fixes.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
///1、连接Redis服务器
redisContext *context = redisConnect("127.0.0.1", 6003);
if (context->err)
{
fprintf(stderr, "Could not connect to Redis at %s:%d: %s\n", "127.0.0.1", 6003, context->errstr);
redisFree(context);
context = NULL;
return REDIS_ERR;
}
///2、校验密码
char *auth = "123456";
redisReply *reply = (redisReply *)redisCommand(context, "AUTH %s", auth);
if (NULL == reply)
{
return REDIS_ERR;
}
else
{
freeReplyObject(reply);
}
///3、选择数据库
reply = (redisReply *)redisCommand(context, "SELECT %d", 1);
if (NULL == reply)
{
return REDIS_ERR;
}
else
{
if (REDIS_REPLY_ERROR == reply->type)
{
return REDIS_ERR;
}
freeReplyObject(reply);
}
while (1)
{
std::string strPublishMessage;
char s[1024];
if (gets(s) == NULL)
{
continue;
}
strPublishMessage.append(s);
///4、推送消息给订阅段
reply = (redisReply *)redisCommand(context, "publish %s %s", "RedisChat", strPublishMessage.data());
if (NULL == reply)
{
return REDIS_ERR;
}
else
{
freeReplyObject(reply);
}
}
redisFree(context);
context = NULL;
return 0;
}
订阅者模式
// ConsoleApplication11.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <hiredis.h>
#define NO_QFORKIMPL
extern "C"
{
#include <win32fixes.h>
}
#include <ICRSINT.H>
#include <windows.h>
#include "win32fixes.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
///1、连接Redis服务器
redisContext *context = redisConnect("127.0.0.1", 6379);
if (context->err)
{
fprintf(stderr, "Could not connect to Redis at %s:%d: %s\n", "127.0.0.1", 6003, context->errstr);
redisFree(context);
context = NULL;
return REDIS_ERR;
}
///2、校验密码
char *auth = "123";
redisReply *reply = (redisReply *)redisCommand(context, "AUTH %s", auth);
if (NULL == reply)
{
return REDIS_ERR;
}
else
{
freeReplyObject(reply);
}
///3、选择数据库
reply = (redisReply *)redisCommand(context, "SELECT %d", 1);
if (NULL == reply)
{
return REDIS_ERR;
}
else
{
if (REDIS_REPLY_ERROR == reply->type)
{
return REDIS_ERR;
}
freeReplyObject(reply);
}
///4、订阅频道
reply = (redisReply *)redisCommand(context, "subscribe %s", "knowledge");
if (NULL == reply)
{
return REDIS_ERR;
}
else
{
freeReplyObject(reply);
}
while (1)
{
///5、阻塞等待 直至取得订阅消息
void *_reply;
if (redisGetReply(context, &_reply) != REDIS_OK)
{
continue;
}
reply = (redisReply*)_reply;
std::string strMessage;
for (int nIndex = 0; nIndex < reply->elements; nIndex++)
{
std::cout << nIndex + 1 << ")";
std::cout << reply->element[nIndex]->str << std::endl;
}
freeReplyObject(reply);
std::cout << "---------------------------------------" << std::endl;
}
redisFree(context);
context = NULL;
return 0;
}
设置include位置D:\Redis\include\src\Win32_Interop和D:\Redis\include\deps\hiredis,lib位置D:\Redis\lib,引入的lib包括:
5、最关键和重要的一步,将win32fixes.c 和win32fixes.h这两个文件加入到自己的工程里。如图:
6、编译
编译过程中也可能会出现很多错误,我基本上都遇到了:
(1)各种宏重定义错误,在预编译头那加以下三个:
(2)各种与其他库的使用冲突,请右击项目->属性->配置属性->C/C++->代码生成->运行库->改成多线程调试(/MTd)或多线程(/MT)
欢迎关注我的blog:wrjgx.cn
引用:
https://blog.csdn.net/u010601662/article/details/76714267
https://blog.csdn.net/xumaojun/article/details/51558128