windows下redis 和 hiredis的编译与使用

果然,高端的程序员真心是鸟都不鸟windows的,redis的客户端找了一圈愣是没有C++的windows版本

我要做个windows上的C++的服务器都没办法和redis交互

github上所有能试的我都试过了,要么是只支持unix,要么是怎么编译都不通过,焦头烂额中

然后我总结了网上无数的教程,附带修复一个个编译错误,总结如下

编译环境,32位windows7,VS2013

获取redis windows版

MS Open Technologies 官方主页

GitHub上的MSOpenTech/redis项目地址

编译 redis:

1.解压到redis(文件夹名字自定义),打开msvs用vs 打开RedisServer.sln项目,编译全部。

2.编译完成后在DUBUG里面找5个EXE文件,分别是redis-benchmark.exe,redis-check-aof.exe,redis-check-dump.exe,redis-cli.exe,redis-server.exe

把5个EXE复制到redis的根目录下即可(与bin文件夹同目录),它们的作用如下:

redis-server:Redis服务器的daemon启动程序

redis-cli:Redis命令行操作工具。也可以用telnet根据其纯文本协议来操作

redis-benchmark:Redis性能测试工具,测试Redis在当前系统下的读写性能

redis-check-aof:数据修复

redis-check-dump:检查导出工具

3.修改redis配置文件,根目录下的redis.conf文件

参数介绍:

daemonize:是否以后台daemon方式运行

pidfile:pid文件位置

port:监听的端口号

timeout:请求超时时间

loglevel:log信息级别

logfile:log文件位置

databases:开启数据库的数量

save * *:保存快照的频率,第一个*表示多长时间,第三个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。

rdbcompression:是否使用压缩

dbfilename:数据快照文件名(只是文件名,不包括目录)

dir:数据快照的保存目录(这个是目录)

appendonly:是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。

appendfsync:appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步)

 

4.启动redis

进入redis目录后 开启服务  (注意加上redis.conf)

redis-server.exe redis.conf 

这个窗口要保持开启  关闭时redis服务会自动关闭redis会自动保存数据到硬盘 

 

5.测试使用

另外开启一个命令行窗口 进入redis目录下 (注意修改自己的ip)

1.redis-cli.exe -h 192.168.10.61 -p 6379 

 

编译 hiredis(其实上面编译全部reids的时候已经编译过的):

1.编译两个lib: hiredis.lib和Win32_Interop.lib

打开从GitHub上clone下来的文件夹,打开里面的msvs文件夹中的RedisServer.sln

从解决方案资源管理器窗口编译hiredis工程和Win32_Interop工程(调试的时候请在debug模式下编译这两个库),此时便会在Debug/Release文件夹下生成这两个工程编译的lib

2.在自己的工程中使用

(1)添加上一步编译的这两个lib到工程中

(2)复制GItHub redis项目文件夹中src/Win32_Interop下所有头文件

(3)以及deps/hiredis下所有头文件(其中fmacros.h用src文件夹下的fmacros.h文件替代)

(4)再复制src/Win32_Interop/win32fixes.c到自己的工程目录,包含到工程文件中

(5)调整各个文件include的路径

(6)示例代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <hiredis.h>
#define NO_QFORKIMPL //这一行必须加才能正常使用
#include <Win32_Interop\win32fixes.h>
#pragma comment(lib,"hiredis.lib")
#pragma comment(lib,"Win32_Interop.lib")

int main()
{
    unsigned int j;
    redisContext *c;
    redisReply *reply;

    struct timeval timeout = { 1, 500000 }; // 1.5 seconds
    c = redisConnectWithTimeout((char*)"127.0.0.1", 6379, timeout);
    if (c->err) {
        printf("Connection error: %s\n", c->errstr);
        exit(1);
    }

    /* PING server */
    reply = (redisReply *)redisCommand(c, "PING");
    printf("PING: %s\n", reply->str);
    freeReplyObject(reply);

    /* Set a key */
    reply = (redisReply *)redisCommand(c, "SET %s %s", "foo", "hello world");
    printf("SET: %s\n", reply->str);
    freeReplyObject(reply);

    /* Set a key using binary safe API */
    reply = (redisReply *)redisCommand(c, "SET %b %b", "bar", 3, "hello", 5);
    printf("SET (binary API): %s\n", reply->str);
    freeReplyObject(reply);

    /* Try a GET and two INCR */
    reply = (redisReply *)redisCommand(c, "GET foo");
    printf("GET foo: %s\n", reply->str);
    freeReplyObject(reply);

    reply = (redisReply *)redisCommand(c, "INCR counter");
    printf("INCR counter: %lld\n", reply->integer);
    freeReplyObject(reply);
    /* again ... */
    reply = (redisReply *)redisCommand(c, "INCR counter");
    printf("INCR counter: %lld\n", reply->integer);
    freeReplyObject(reply);

    /* Create a list of numbers, from 0 to 9 */
    reply = (redisReply *)redisCommand(c, "DEL mylist");
    freeReplyObject(reply);
    for (j = 0; j < 10; j++) {
        char buf[64];

        sprintf_s(buf, 64, "%d", j);
        reply = (redisReply *)redisCommand(c, "LPUSH mylist element-%s", buf);
        freeReplyObject(reply);
    }

    /* Let's check what we have inside the list */
    reply = (redisReply *)redisCommand(c, "LRANGE mylist 0 -1");
    if (reply->type == REDIS_REPLY_ARRAY) {
        for (j = 0; j < reply->elements; j++) {
            printf("%u) %s\n", j, reply->element[j]->str);
            getchar();
        }
    }
    freeReplyObject(reply);

    return 0;
}

PS.可能会碰到的编译错误

 

1.必须定义入口点,请在win32fixes.h之前加上#define NO_QFORKIMPL

2.各种与其他库的使用冲突,请右击项目->属性->配置属性->C/C++->代码生成->运行库->改成多线程调试(/MTd)或多线程(/MT)

并且在右击项目->属性->配置属性->连接器->命令行中输入/NODEFAULTLIB:libcmt.lib

3.error C4996,各种unsafe报错啊,请右击项目->属性->配置属性->C/C++->预处理器->预处理器定义->添加“_CRT_SECURE_NO_WARNINGS”(不带引号)

posted @ 2015-03-26 14:42  Sky_Raker  阅读(14594)  评论(1编辑  收藏  举报