c++鏈接redis(vs2015)

(注:整個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)

“`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章