cpp_redis (Windows C++ Redis客户端静态库,C++11实现)源码编译及使用

一、环境准备

win7,VS2015

https://github.com/Cylix/cpp_redis   v4.3.1

https://github.com/Cylix/tacopie      v3.2.0

https://cylix.github.io/cpp_redis/html/classcpp__redis_1_1client.html

把cpp_redis和tacopie的源码下载之后,把tacopie源码解压到路径

C:\Users\Administrator\Documents\Visual Studio 2015\Projects\cpp_redis-4.3.1\tacopie\

vs2015打开cpp_redis的工程,

C:\Users\Administrator\Documents\Visual Studio 2015\Projects\cpp_redis-4.3.1\msvc15\cpp_redis.sln

debug和release分别编译,最终会生成debug和release版本的lib库:

tacopie.lib

cpp_redis.lib

两个文件都很大,分别是10MB和50MB左右。。。


二、提取出头文件和静态库文件,分别新建文件夹includes和libs

1、把C:\Users\Administrator\Documents\Visual Studio 2015\Projects\cpp_redis-4.3.1\includes\cpp_redis和C:\Users\Administrator\Documents\Visual Studio 2015\Projects\cpp_redis-4.3.1\tacopie\includes\tacopie两个文件夹都拷贝到includes

--includes

    --cpp_redis

    --tacopie

2、把debug和release的tacopie.lib和cpp_redis.lib拷贝进入libs

--libs

     --debug

             --tacopie.lib

             --cpp_redis.lib

     --release

             --tacopie.lib

             --cpp_redis.lib


三、新建win32工程,实测

测试源码主要参考了,https://github.com/Cylix/cpp_redis/tree/master/examples/cpp_redis_client.cpp

本人添加了静态库的指向和redis auth的校验

另外,VS2015工程属性,配置属性,C/C++,附加包含目录写入“..\includes”

// redisclientwin32.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include   
#include   
//#include 
//#include 

#ifdef _WIN32  
#include   
#endif /* _WIN32 */  

#ifdef _DEBUG
#pragma comment(lib, "../libs/debug/tacopie.lib")
#pragma comment(lib, "../libs/debug/cpp_redis.lib")
#else
#pragma comment(lib, "../libs/release/tacopie.lib")
#pragma comment(lib, "../libs/release/cpp_redis.lib")
#endif


int main()
{
#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(new cpp_redis::logger);

	cpp_redis::client client;

	client.connect("172.16.6.138", 6379, [](const std::string& host, std::size_t port, cpp_redis::client::connect_state status) {
		if (status == cpp_redis::client::connect_state::dropped) {
			std::cout << "client disconnected from " << host << ":" << port << std::endl;
		}
	});

	// auth,password
	client.auth("123456", [](cpp_redis::reply& reply) {
		std::cout << "auth info: " << reply << std::endl;
		// if (reply.is_string())
		//   do_something_with_string(reply.as_string())
	});

	// 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;
}


完整的源码下载:

https://download.csdn.net/download/libaineu2004/10290808




你可能感兴趣的:(C/C++,redis)