Redis客户端:C/C++ hiredis

摘要

本篇博客对Redis基于C/C++的客户端工具hiredis进行简单介绍,以便加深理解和记忆

1.Linux下安装hiredis

1)方式一:apt

# 查询c语言对应的开发库
apt-cache search hiredist
# 下载安装
sudo apt-get install libhiredis-dev

hiredis库目录的默认位置为/usr/lib/x86_64-linux-gnu/下,头文件在/usr/include/hiredis中,hiredis头文件中定义了各种API

2)方式二:源码编译

下载hiredis源码

# 解压后编译
make
sudo make install

2.Windows下安装hiredis

  • 下载微软发布的windows下的redis源码(在官网下载发布版的zip包更稳定)
git clone https://github.com/microsoftarchive/redis.git
  • 用新版的Visual Studio打开工程
  • 打开时对该项目之前使用的Visual Studio工具集进行版本升级,以适配当前的版本的Visual Studio
  • 编译Redis源码
  • 错误修复:编译过程中会报system_error:不是"std"的成员变量system_error:找不到标识符system_category:找不到标识符

去掉std::、在报找不到标识符的文件中引入#include <system_error>头文件

  • 重新编译:成功
  • 将编译后的静态链接文件导入到项目的libs文件夹下,并在Visual Studio中增加链接
  • redis\deps\hiredisredis\src\Win32_Interop目录放入项目的include文件夹中(或与项目源文件同级目录下)并进行适配
  • 启动redis-server.exe服务端,并进行连接测试
// TestRedis.cpp : 定义控制台应用程序的入口点。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/hiredis.h>
//#include <Win32_Interop\win32fixes.h>

int main() {
	unsigned int j;
	redisContext* c;
	redisReply* reply;
	c = redisConnect("127.0.0.1", 6379);
	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);
	}
	else {
		printf("connect redis successed!\n");
	}

	/* 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);

	for (j = 0; j < 5; j++)
	{
		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, "%u", 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);

	system("pause");
	return 0;
}

3.其他C/C++的Redis客户端

客户端最后更新支持Linux支持Windows支持pub/sub支持redis版本备注
C++ Client2011-03-03×2.0
c+redis+client2016-09-30自加工A redis client based on hiredis, supports cluster/pipeline and is thread safe and includes two files only. The transaction is on the way )
cpp_redis2016-10-26C++11 Lightweight Redis client: async, thread-safe, no dependency, pipelining, multi-platform.
qredisclient2016-11-04Asynchronous Qt-based Redis client with SSL and SSH tunnelling support.
r3c2016-11-11×A Redis Cluster C++ Client, based on hiredis and support standalone, it’s easy to make and use, not depends on C++11 or later.
redis-client for C++2016-11-07Full Redis client commands, one redis command, one redis function
redis3m2015-10-20×A C++ wrapper of hiredis, with also connection pooling, high availability and ready-to-use patterns
redisclient2016-10-12A C++ asynchronous client based on boost::asio
redox2016-07-29×Modern, asynchronous, and fast C++11 client for Redis
SimpleRedisClient2015-03-14×2.6/2.8a very simple Redis client for C++
xredis2016-10-14Redis C++ client with data slice storage, connection pool, master slave connection, read/write separation; requires hiredis only
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值