封装hiredis成dll包,为老项目提供redis网络支持

第一步:准备VS环境

1、需要下载window8.1的SDK否则无法下载

2、平台工具集需要使用Visual Studio 2015(v140)

第二步:下载hiredis

去microsoft/hiredis下载windows版本的hiredis,并解压到本地

打开hiredis-master\msvs\vs-solutions中的sln,点击生成。

将会得到如下的文件:

第三步:动态链接库(DLL)

1、创建DLL工程

流程:文件 -> 新建 -> 项目 -> 动态链接库(DLL)-> Next - > 填写项目名。

图1:DLL项目创建

图2:DLL项目名设置

2、等待项目生成

等待项目生成后项目结构如下图。

3、配置依赖

点击项目按右键,选择在文件夹中打开项目

项目结构如下所示(其中Debug\、x64\文件夹是生成的,可忽略):

新建lib文件夹include文件夹和。lib文件夹包含上一步得到的.lib文件和.pdb文件。include文件夹包含hiredis\,win32_interop\和adapters\三个文件夹,分别对应hiredis-master\,msvs\win32_interop\和hiredis\adapters\下所有的.h文件。

添加包含目录、库目录:

添加额外的依赖项:

添加编译头:

在预处理器定义中添加如下字符:_CRT_SECURE_NO_WARNINGS

4、其它配置

修改代码生成设置:

在C/C++中选择代码生成,并选择运行库,在运行库中选择MDd模式。

在配置属性常规中选择配置类型为动态库。

设置平台SDK

设置平台工具集

第三步:撰写代码并编译

RedisUtils.h

cpp 复制代码
extern "C" REDISUTILS_API int testRedis(char* host, int input_port);

RedisUtils.cpp

cpp 复制代码
REDISUTILS_API int testRedis(char* host, int input_port) {
	
	
	unsigned int j;
	redisContext* c;
	redisReply* reply;
	const char* hostname = host;
	int port = input_port;
	struct timeval timeout = { 1, 500000 }; // 1.5 seconds
	c = redisConnectWithTimeout(hostname, port, timeout);
	if (c == NULL || c->err) {
		if (c) {
			printf("Connection error: %s\n", c->errstr);
			redisFree(c);
		}
		else {
			printf("Connection error: can't allocate redis context\n");
		}
		exit(1);
	}

	/* PING server */
	reply = (redisReply*)redisCommand(c, "PING");
	printf("PING: %s\n", reply->str);
	freeReplyObject(reply);

	/* Set a key */
	reply = (redisReply*)redisCommand(c, "SET %s %s", "foo", "hello world");
	printf("SET: %s\n", reply->str);
	freeReplyObject(reply);

	/* Set a key using binary safe API */
	reply = (redisReply*)redisCommand(c, "SET %b %b", "bar", (size_t)3, "hello", (size_t)5);
	printf("SET (binary API): %s\n", reply->str);
	freeReplyObject(reply);

	/* Try a GET and two INCR */
	reply = (redisReply*)redisCommand(c, "GET foo");
	printf("GET foo: %s\n", reply->str);
	freeReplyObject(reply);

	reply = (redisReply*)redisCommand(c, "INCR counter");
	printf("INCR counter: %lld\n", reply->integer);
	freeReplyObject(reply);
	/* again ... */
	reply = (redisReply*)redisCommand(c, "INCR counter");
	printf("INCR counter: %lld\n", reply->integer);
	freeReplyObject(reply);

	/* Create a list of numbers, from 0 to 9 */
	reply = (redisReply*)redisCommand(c, "DEL mylist");
	freeReplyObject(reply);
	for (j = 0; j < 10; j++) {
		char buf[64];

		snprintf(buf, 64, "%d", j);
		reply = (redisReply*)redisCommand(c, "LPUSH mylist element-%s", buf);
		freeReplyObject(reply);
	}

	/* Let's check what we have inside the list */
	reply = (redisReply*)redisCommand(c, "LRANGE mylist 0 -1");
	if (reply->type == REDIS_REPLY_ARRAY) {
		for (j = 0; j < reply->elements; j++) {
			printf("%u) %s\n", j, reply->element[j]->str);
		}
	}
	freeReplyObject(reply);

	/* Disconnects and frees the context */
	redisFree(c);

	return 0;
}

编译代码:

右击项目点击生成

生成完毕后会在Debug目录下生成对应的dll文件

项目地址:

下面是项目的github地址,大家可以去看看:GitHub - 1756336885/RedisUtils: C++封装的DLL包,可以为一些老项目提供redis支持

参考文章:

Windows下使用hiredis各种常见问题(折磨死人了)_hiredis windows_无极之剑的博客-CSDN博客

C++编写Config类读取配置文件_vc++ 判断config 在不在_David_xtd的博客-CSDN博客

无法打开源文件 ctype.h和.exe 进行写入_无法打开源文件ctype.h_河西石头的博客-CSDN博客

hiredis在windows下的编译以及使用_hiredis windows_持久决心的博客-CSDN博客

redis++的编译,安装,使用_redis++库_YZF_Kevin的博客-CSDN博客

https://www.cnblogs.com/javaxubo/p/16684647.html

相关推荐
Escalating_xu19 分钟前
【MySQL】索引原理(上):从数据页到 B+ 树,弄懂聚簇索引、回表与覆盖索引
数据库·mysql
FungLeo31 分钟前
FengCMS ·PHP+MySQL升级实录 · 富文本清洗配置实战:白名单、URI scheme、禁 iframe/id、实例缓存
mysql·缓存·php·xss·fengcms
DataY轻量数据平台1 小时前
MongoDB 怎么做 CDC?不用 Debezium,Change Streams 就是它的“binlog”
数据库·mongodb
djjjx.2 小时前
Mysql数据库基础篇(十):从磁盘 IO 到 B+ 树:图解 MySQL 索引的底层演进、Page 结构、目录机制与 IO 优化原理
数据库·mysql
En^_^Joy2 小时前
LangChain消息系统全解析:从基础到实战(四)
数据库·microsoft·langchain
CoderYanger3 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第三弹):统一数据返回格式+统一异常处理
java·数据库·spring boot·后端·spring·java-ee·mybatis
飞奔的瓜牛3 小时前
运维工程师 Redis 日常巡检清单与实战注意事项
运维·数据库·redis
龙腾AI白云3 小时前
视觉运动控制与灵巧抓取:具身智能手眼协调的学习
大数据·数据库·人工智能·机器学习·pygame
IT规划师3 小时前
Redis 核心机制 | 发布订阅:Pub_Sub 与 Sharded PubSub
redis·.net
多弗朗皮卡丘12 小时前
MySQL表的约束
数据库·mysql