【1】 Windows C++ Redis客戶端 cpp_redis(C++11實現)官方最新源碼編譯 【最後更新2018-10-31】

該資源本人整理製作,知識付費,請尊重勞動者的成果 

聯繫 我的QQ就是我的CSDN頭像

友情提醒:該庫必須使用Visual Studio 2015及以上版本纔可以編譯通過!因爲用到了C++11的很多新特性。

場景:

有時候我們需要在Windows下用C++訪問redis服務(不管redis服務是Windows的還是Linux的都可以)

特點:

cpp_redis需要C++11特性

代碼流暢,簡潔明瞭

後續文章:

【2】Windows C++ Redis服務端搭建與客戶端開發

【3】RedisClient Redis數據瀏覽神器(支持對Redis數據進行界面化操作)

【4】cpp_redis hello world

【5】cpp_redis reply

【6】cpp_redis exists

【7】cpp_redis hash容器 增刪改查 (以及避免死鎖的解決辦法)

【7-A】cpp_redis send萬能指令

【8】cpp_redis hash容器 增刪改查 結果賦值給本地對象

【9】cpp_redis RapidJson redis (C++對象利用Rapidjson序列化到redis與反序列化)

資源:

使用官方網站最新源碼編譯http://redis.io/clients    https://github.com/cylix/cpp_redis

更新:2018-09-23 (Debug | Release) * (Win32 | x64) 均可直接編譯,示例代碼比較全面,各種操作都有

cpp_redis_master   sln

已經編譯好的調用工程

use_cpp_redis sln

編譯及使用教程

Windows下cpp_redis_master編譯與使用.docx (自己可以從頭開始重現,方便自己學習使用)

 

百度雲:

 

客戶端代碼:

 

#include <cpp_redis/cpp_redis>

#include <iostream>

#ifdef _WIN32
#include <Winsock2.h>
#endif /* _WIN32 */

int
main(void) {
#ifdef _WIN32
  //! Windows netword DLL init
  WORD version = MAKEWORD(2, 2);
  WSADATA data;

  if (WSAStartup(version, &data) != 0) {
    std::cerr << "WSAStartup() failure" << std::endl;
    return -1;
  }
#endif /* _WIN32 */

  //! Enable logging
  cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger);

  cpp_redis::redis_client client;

  client.connect("127.0.0.1", 6379, [](cpp_redis::redis_client&) {
    std::cout << "client disconnected (disconnection handler)" << std::endl;
  });

  // same as client.send({ "SET", "hello", "42" }, ...)
  client.set("hello", "42", [](cpp_redis::reply& reply) {
    std::cout << "set hello 42: " << reply << std::endl;
    // if (reply.is_string())
    //   do_something_with_string(reply.as_string())
  });

  // same as client.send({ "DECRBY", "hello", 12 }, ...)
  client.decrby("hello", 12, [](cpp_redis::reply& reply) {
    std::cout << "decrby hello 12: " << reply << std::endl;
    // if (reply.is_integer())
    //   do_something_with_integer(reply.as_integer())
  });

  // same as client.send({ "GET", "hello" }, ...)
  client.get("hello", [](cpp_redis::reply& reply) {
    std::cout << "get hello: " << reply << std::endl;
    // if (reply.is_string())
    //   do_something_with_string(reply.as_string())
  });

  // commands are pipelined and only sent when client.commit() is called
  // client.commit();

  // synchronous commit, no timeout
  client.sync_commit();

// synchronous commit, timeout
// client.sync_commit(std::chrono::milliseconds(100));

#ifdef _WIN32
  WSACleanup();
#endif /* _WIN32 */

  return 0;
}

運行效果

用戶體驗

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