记vs2012 C++ 连接redis

5 篇文章 0 订阅

注意
vs2012 编译redis时,尽量选用 redis2.6版本

win版本下载地址: https://github.com/MicrosoftArchive/redis

  1. vs2012编译redis工程,生成x86和x64版本 hiredis.lib
  2. 建立vs项目工程
    • vs工程下建立 hiredis目录,再建立子目录 includelibs,分别用来存放链接所有的头文件及lib库
    • redis-2.6\src 中头文件win32fixes.hredis-2.6\deps\hiredis 中所有头文件拷贝到 include 目录中
    • 将生成的 hiredis.lib拷贝到 libs 目录中
    • redis-2.6\srcwin32fixes.c 文件拷贝到 vs项目下
  3. 修改vs工程配置属性
    • 右击项目->属性->配置属性->C/C+±>代码生成->运行库->改成多线程调试(/MTd)或多线程(/MT)
    • 右击项目->属性->配置属性->链接器->命令行中输入 /NODEFAULTLIB:libcmt.lib
    • 右击项目->属性->配置属性->C/C+±>预处理器->预处理器定义->添加 _CRT_SECURE_NO_WARNINGS
  4. 添加测试cpp代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

void doTest()
{
    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;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值