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

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: 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 <cpp_redis/cpp_redis>  
#include <iostream>  
//#include <algorithm>
//#include <iterator>
#ifdef _WIN32  
#include <Winsock2.h>  
#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<cpp_redis::logger>(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

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1月前
|
存储 算法 C++
Windows共享文件:探秘C++实现的B树索引算法奇境
在数字化时代,Windows共享文件的高效管理至关重要。B树算法以其自平衡多路搜索特性,在文件索引与存储优化中表现出色。本文探讨B树在Windows共享文件中的应用,通过C++实现具体代码,展示其构建文件索引、优化数据存储的能力,提升文件检索效率。B树通过减少磁盘I/O操作,确保查询高效,为企业和个人提供流畅的文件共享体验。
|
4月前
|
自然语言处理 编译器 C语言
为什么C/C++编译腰要先完成汇编
C/C++ 编译过程中先生成汇编语言是历史、技术和实践的共同选择。历史上,汇编语言作为成熟的中间表示方式,简化了工具链;技术上,分阶段编译更高效,汇编便于调试和移植;实践中,保留汇编阶段降低了复杂度,增强了可移植性和优化能力。即使在现代编译器中,汇编仍作为重要桥梁,帮助开发者更好地理解和优化代码。
79 25
为什么C/C++编译腰要先完成汇编
|
4月前
基于springboot+thymeleaf+Redis仿知乎网站问答项目源码
基于springboot+thymeleaf+Redis仿知乎网站问答项目源码
207 36
|
6月前
|
自然语言处理 编译器 Linux
告别头文件,编译效率提升 42%!C++ Modules 实战解析 | 干货推荐
本文中,阿里云智能集团开发工程师李泽政以 Alinux 为操作环境,讲解模块相比传统头文件有哪些优势,并通过若干个例子,学习如何组织一个 C++ 模块工程并使用模块封装第三方库或是改造现有的项目。
593 56
|
6月前
|
自然语言处理 编译器 Linux
|
7月前
|
存储 程序员 编译器
简述 C、C++程序编译的内存分配情况
在C和C++程序编译过程中,内存被划分为几个区域进行分配:代码区存储常量和执行指令;全局/静态变量区存放全局变量及静态变量;栈区管理函数参数、局部变量等;堆区则用于动态分配内存,由程序员控制释放,共同支撑着程序运行时的数据存储与处理需求。
374 22
|
7月前
|
缓存 NoSQL Ubuntu
大数据-39 Redis 高并发分布式缓存 Ubuntu源码编译安装 云服务器 启动并测试 redis-server redis-cli
大数据-39 Redis 高并发分布式缓存 Ubuntu源码编译安装 云服务器 启动并测试 redis-server redis-cli
120 3
|
7月前
|
Linux 编译器 C语言
Linux c/c++之多文档编译
这篇文章介绍了在Linux操作系统下使用gcc编译器进行C/C++多文件编译的方法和步骤。
102 0
Linux c/c++之多文档编译
|
7月前
|
算法 编译器 C++
【C++篇】领略模板编程的进阶之美:参数巧思与编译的智慧
【C++篇】领略模板编程的进阶之美:参数巧思与编译的智慧
153 2
|
7月前
|
并行计算 开发工具 异构计算
在Windows平台使用源码编译和安装PyTorch3D指定版本
【10月更文挑战第6天】在 Windows 平台上,编译和安装指定版本的 PyTorch3D 需要先安装 Python、Visual Studio Build Tools 和 CUDA(如有需要),然后通过 Git 获取源码。建议创建虚拟环境以隔离依赖,并使用 `pip` 安装所需库。最后,在源码目录下运行 `python setup.py install` 进行编译和安装。完成后即可在 Python 中导入 PyTorch3D 使用。
731 0