hiredis的安装与使用
- 介绍
hiredis是redis数据库的简约C客户端库,是redis官方的C语言客户端,支持所有命令(command set),管道(pipelining),时间驱动编程(event driven programming)。
hiredis的安装
在redis的发行包中的deps目录中就包含hiredis的源码,手动编译安装,或者自行下载一份。https://github.com/redis/hiredis
cd /deps/hiredis
make
make install
mkdir /usr/lib/hiredis
cp libhiredis.so /usr/lib/hiredis //将动态连接库libhiredis.so至/usr/lib/hiredis
mkdir /usr/include/hiredis
cp hiredis.h /usr/include/hiredis //头文件包含#include<hiredis/hiredis.h>
- hiredis的API
redisContext *redisConnect(const char *ip, int port);
void *redisCommand(redisContext *c, const char *format, ...);
4.1 连接 redisConnect
redisConnect函数用于创建一个所谓的redisContext,第一个参数传递一个ip,第二个传递端口。尝试使用redisConnect连接到Redis redisConnect您应该检查err字段以查看建立连接是否成功。
redisContext的结构如下:
typedef struct redisContext {
int err; /*错误标志,正确连接标志为0,出错时设置为非零常量*/
char errstr[128]; /*存放错误信息的字符串*/
int fd;
int flags;
char *obuf; /* Write buffer */
redisReader *reader; /* Protocol reader */
} redisContext;
//redisContext不是线程安全的
设置错误的非零常量有:
REDIS_ERR_IO:创建连接时出现I / O错误,尝试写入套接字或从套接字读取。
REDIS_ERR_EOF:服务器关闭导致空的读取的连接。
REDIS_ERR_PROTOCOL:解析协议时出错。
REDIS_ERR_OTHER:任何其他错误。 目前,仅当指定的连接主机名无法解析时才使用。
4.2 发送命令 redisCommand
redisCommand函数是一个可变参函数,格式与printf类似,第一个参数传递一个redisContext的地址,由redisConnect函数返回,第二个参数例如:说明符%s,第三个参数就是代替%s的字符串。
如果命令执行错误,返回值为NULL,redisContext的err字段被设置为非零常量。如果,错误发生,原先的redisContext就不能重复使用,需要重新建立一个新的连接。如果成功执行命令,则标准返回一个redisReply类型,该类型结构如下:
typedef struct redisReply {
int type; /* 测试收到什么样的回返回 REDIS_REPLY_* */
long long integer; /* type 是 REDIS_REPLY_INTEGER 类型, integer保存返回的值*/
int len; /* 保存str类型的长度 */
char *str; /* type 是 REDIS_REPLY_ERROR 和 REDIS_REPLY_STRING,str保存返回的值 */
size_t elements; /* type 是 REDIS_REPLY_ARRAY,保存返回多个元素的数量 */
struct redisReply **element; /* 返回多个元素以redisReply对象的形式存放 */
} redisReply;
//type还可以是REDIS_REPLY_NIL,表示返回了一个零对象,没有数据可以访问。
4.3 释放redisReply的函数freeReplyObject
当使用完redisReply后,调用freeReplyObject函数释放空间。
void freeReplyObject(void *reply);
4.4释放redisContext的函数redisFree
断开连接并释放redisContext空间。
void redisFree(redisContext *c);
例子:
5. API的使用
#include <stdio.h>
#include <string.h>
#include <hiredis/hiredis.h>
void test(void)
{
redisContext *context = redisConnect("127.0.0.1", 6379);//默认端口,本机redis-server服务开启
if(context->err) {
redisFree(context);
printf("connect redisServer err:%s\n", context->errstr);
return ;
}
printf("connect redisServer success\n");
const char *cmd = "SET test 100";
redisReply *reply = (redisReply *)redisCommand(context, cmd);
if(NULL == reply) {
printf("command execute failure\n");
redisFree(context);
return ;
}
//返回执行结果为状态的命令。比如set命令的返回值的类型是REDIS_REPLY_STATUS,然后只有当返回信息是"OK"时,才表示该命令执行成功。可以通过reply->str得到文字信息
if(!(reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "OK") == 0)) {
printf("command execute failure:%s\n", cmd);
freeReplyObject(reply);
redisFree(context);
return ;
}
freeReplyObject(reply);
printf("%s execute success\n", cmd);
const char *getVal = "GET test";
reply = (redisReply *)redisCommand(context, getVal);
if(reply->type != REDIS_REPLY_STRING)
{
printf("command execute failure:%s\n", getVal);
freeReplyObject(reply);
redisFree(context);
return ;
}
printf("GET test:%s\n", reply->str);
freeReplyObject(reply);
redisFree(context);
}
int main(void)
{
test();
return 0;;
}
执行结果:
➜ REDIS gcc test.c -lhiredis //编译链接
➜ REDIS ./a.out
connect redisServer success
SET test 100 execute success //执行"SET test 100"命令
GET test:100 //得到test的值
➜ REDIS redis-cli //命令行开启redis-cli客户端
127.0.0.1:6379> GET test //使用命令查看test值
"100"