windows下使用c++调用redis

unix下c++调用 redis可以看这个:

http://blog.csdn.net/youngqj/article/details/8266177

 ==================================================================================

redis的官网版本并没有为vc开发提供接口,不过微软对redis好像很感兴趣,自己弄了一个 ,完整的英文说明在这里:

https://gist.github.com/MS-Interop/1439660    根据说明,一套完整下来,你就可以自己搭一个VC版本的 redis。

因为流程比较复杂,怕以后自己要用又忘记,趁记得写下来。

==========================================================================================

下面的步骤其实就是要弄出  MSOpenTech/redis(https://github.com/MSOpenTech/redis)里面的redis/msvs中的sln,链接中有下载,但我打不开,如果你能打开请无视下面的,直接使用。

或者下载这个:http://download.csdn.net/detail/biantaiwangzi/7864413

==========================================================================================

1.首先要先配置好git ,详细的内容在这里:http://www.cnblogs.com/sixbeauty/p/3954223.html

 

2.新建一个文件夹(名为redis_build好了),打开cmd,cd进去,使用git弄一个antirez/redis的备份。

1
git clone https://github.com/antirez/redis.git

 

3.接下来的几个命令照打就好:

1
2
cd redis
git checkout 3fac86ff1d
1
git checkout -b 2.4_win_uv

 

4.下载redis24_win_uv.patch,(其实就是英文说明最下面那个。)必须要先下载才能执行成功。

把 (redis24_win_uv.patch) 拉到之前创建的目录redis_bulid下的redis里面,执行:

1
git am redis24_win_uv.patch

 如果有下面的warning提示可以忽略:

1
2
warning: squelched 210 whitespace errors
warning: 215 lines add whitespace errors.

  

5.继续执行:

1
curl https://raw.github.com/gist/1439660/d729b823a7ef50ef8ba54393675fb678e740ca4b/redis24_win_uv.patch | git am

到这一步执行完,在redis文件夹下面的msvs里面,我们就能得到RedisServer.sln文件。

但现在还是没用搞定。

6.下载:ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-8-0-release.exe,执行。在redis/deps 下 建 pthreads-win32 文件夹。

  6.1、把pre-built.2的include复制到 pthreads-win32里面。

  6.2、把pre-built.2的lib中的   "pthreadVC2.dll"和"pthreadVC2.lib" 复制到 pthreads-win32/lib/debug 中,并把 "pthreadVC2.lib"改名为"pthread.lib" 。(如果是release版就复制到 pthreads-win32/lib/release  中)

 

7.现在可以打开 RedisServer.sln 编译生成了。

 

======================================分割线================================================

使用:

编译完成后,在msvs中的Debug中有hiredis的lib,使用它我们就能建立windows下redis的c++开发环境了:

1.配置:

a. 添加包含目录

【项目->属性->配置属性->VC++ 目录->包含目录】  中添加两个文件目录:  

  **/redis/src;**/redis/deps/hiredis  

注:这两个文件就是刚刚我们的sln目录中的

 

b. 添加库目录

【项目->属性->配置属性->VC++ 目录->库目录】添加   **/redis\msvs\Debug

 

c. 添加依赖库

项目->属性->链接器->输入->附加依赖项->ws2_32.lib;hiredis.lib;

 

d. 最后把/**/redis/src/下的win32fixes.c放到项目目录下(即main.cpp文件所在位置)

 

2.使用:

复制代码
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include "hiredis.h"

void doTest()
{
    //redis默认监听端口为6387 可以再配置文件中修改
    redisContext* c = redisConnect("127.0.0.1", 6379);
    if ( c->err)
    {
        printf("Connect to redisServer faile:%s\n",c->errstr);
        redisFree(c);
        return ;
    }
    printf("Connect to redisServer Success\n");

    const char* command1 = "set stest1 value1";
    redisReply* r = (redisReply*)redisCommand(c, command1);

    if( NULL == r)
    {
        printf("Execut command1 failure\n");
        redisFree(c);
        return;
    }
    if( !(r->type == REDIS_REPLY_STATUS && (strcmp(r->str,"OK")==0 || strcmp(r->str,"ok")==0 ) ))
    {
        printf("Failed to execute command[%s]\n",command1);
        freeReplyObject(r);
        redisFree(c);
        return;
    }    
    freeReplyObject(r);
    printf("Succeed to execute command[%s]\n", command1);

    const char* command2 = "strlen stest1";
    r = (redisReply*)redisCommand(c, command2);
    if ( r->type != REDIS_REPLY_INTEGER)
    {
        printf("Failed to execute command[%s]\n",command2);
        freeReplyObject(r);
        redisFree(c);
        return;
    }
    int length =  r->integer;
    freeReplyObject(r);
    printf("The length of 'stest1' is %d.\n", length);
    printf("Succeed to execute command[%s]\n", command2);


    const char* command3 = "get stest1";
    r = (redisReply*)redisCommand(c, command3);
    if ( r->type != REDIS_REPLY_STRING)
    {
        printf("Failed to execute command[%s]\n",command3);
        freeReplyObject(r);
        redisFree(c);
        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(c, command4);
    if ( r->type != REDIS_REPLY_NIL)
    {
        printf("Failed to execute command[%s]\n",command4);
        freeReplyObject(r);
        redisFree(c);
        return;
    }
    freeReplyObject(r);
    printf("Succeed to execute command[%s]\n", command4);    


    redisFree(c);

}

int main()
{
    WSADATA wsaData;
    int nRet;
    if((nRet = WSAStartup(MAKEWORD(2,2),&wsaData)) != 0){
        printf("WSAStartup failed\n");
        exit(0);
    }
    doTest();
    return 0;
}
复制代码

 

 

redis C接口hiredis 简单函数使用介绍:http://www.cnblogs.com/sixbeauty/p/3955581.html

参考:

Redis在Windows下编译 :http://blog.chinaunix.net/uid-15063109-id-3063848.html

Redis在Windows上编译(Visual C++2010):http://blog.sina.com.cn/s/blog_73c52fda01011c72.html

posted on 2019-02-12 13:32  &大飞  阅读(2446)  评论(0编辑  收藏  举报

导航