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)方式二:源码编译
# 解压后编译
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\hiredis
和redis\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++ Client | 2011-03-03 | √ | × | 2.0 | ||
c+redis+client | 2016-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_redis | 2016-10-26 | √ | √ | C++11 Lightweight Redis client: async, thread-safe, no dependency, pipelining, multi-platform. | ||
qredisclient | 2016-11-04 | √ | Asynchronous Qt-based Redis client with SSL and SSH tunnelling support. | |||
r3c | 2016-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-07 | √ | √ | √ | Full Redis client commands, one redis command, one redis function | |
redis3m | 2015-10-20 | √ | × | A C++ wrapper of hiredis, with also connection pooling, high availability and ready-to-use patterns | ||
redisclient | 2016-10-12 | √ | √ | √ | A C++ asynchronous client based on boost::asio | |
redox | 2016-07-29 | √ | × | √ | Modern, asynchronous, and fast C++11 client for Redis | |
SimpleRedisClient | 2015-03-14 | √ | × | 2.6/2.8 | a very simple Redis client for C++ | |
xredis | 2016-10-14 | √ | √ | √ | Redis C++ client with data slice storage, connection pool, master slave connection, read/write separation; requires hiredis only |