该资源本人整理制作,知识付费,请尊重劳动者的成果
联系 我的QQ就是我的头像
友情提醒:该库必须使用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; }运行效果
用户体验
