redis-hiredis 的安装
文章目录
-
-
- [redis-hiredis 的安装](#redis-hiredis 的安装)
- [核心 API 说明:](#核心 API 说明:)
- 测试代码
-
下载地址:https://github.com/redis/hiredis
解压压缩包:
shell
unzip redis-hiredis-v0.13.3-52-g5f98e1d.zip
进入解压后的目录,编译源码:make
shell
make
安装:
shell
sudo make install
在源码包目录下,有一个 examples 的目录,里面放了一些客户端示例代码:

核心 API 说明:
连接redis数据库
cpp
redisContext *redisConnect(const char *ip, int port);
功能:连接redis数据库
参数:
ip:ip地址
port:端口,一般redis数据库的端口为6379
返回值:
成功:连接句柄redisContext
失败:NULL
执行的是redis数据库中的操作命令
cpp
void *redisCommand(redisContext *c, const char *format, ...);
功能:执行的是redis数据库中的操作命令
参数:
c:连接数据库时 返回的redisContext
剩下的参数:剩下的参数为变参, 就如C标准函数printf函数一样的变参
返回值:
成功:void*, 一般强制转换成为redisReply类型的进行进一步的处理
失败:NULL
shell
/* This is the reply object returned by redisCommand() */
typedef struct redisReply {
int type; /* REDIS_REPLY_* */
long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
size_t len; /* Length of string */
char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;
| 状态标识 | 含义 |
|---|---|
| REDIS_REPLY_STATUS | 表示状态,内容通过 str 字段查看,字符串长度是 len 字段 |
| REDIS_REPLY_ERROR | 表示出错,查看出错信息,如上的 str,len 字段 |
| REDIS_REPLY_INTEGER | 返回整数,从 integer 字段获取值 |
| REDIS_REPLY_NIL | 没有数据返回 |
| REDIS_REPLY_STRING | 返回字符串,查看 str,len 字段 |
| REDIS_REPLY_ARRAY | element[index] 的方式访问数组元素,每个数组元素是一个 redisReply 对象的指针。 |
cpp
void freeReplyObject(void *reply);
功能:释放redisCommand执行后返回的redisReply所占用的内存
参数:redisCommand执行后返回的redisReply
返回值:无
redisReply 返回结果处理,错误类型可通过 redisReply 的 errstr 字段查看简短的描述:
| 状态标识 | 含义 |
|---|---|
| REDIS_OK | 正常 |
| REDIS_ERR_IO | IO 读/写出现异常,通过 errno 查看原因 |
| REDIS_ERR_EOF | 服务器关闭了链接,读结束 |
| REDIS_ERR_PROTOCOL | 分析 redis 协议内容出错 |
| EDIS_ERR_OTHER | 其他未知的错误 |
cpp
void redisFree(redisContext *c);
功能:释放redisConnect()所产生的连接
参数:redisConnect()所产生的连接
返回值:无
测试代码
c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <hiredis/hiredis.h>
int main()
{
redisContext *c = NULL;
//连接到Redis数据库
c = redisConnect("127.0.0.1", 6379);
if (NULL == c || c->err != 0)
{
printf("RedisConnect failed: %s\n", c->errstr);
return 1;
}
printf("连接到Redis成功\n");
//执行Redis命令
reply = redisCommand(c, "PING");
if (NULL == reply)
{
printf("执行命令失败....\n");
return 1;
}
if (reply->type == REDIS_REPLY_STATUS)
{
printf("执行结果: %s\n", reply->str);
}
//断开连接和释放内存
redisFree(c);
return 0;
}
执行set命令
c
int main()
{
redisContext *c = NULL;
redisReply *reply = NULL;
//连接到Redis数据库
c = redisConnect("127.0.0.1", 6379);
if (NULL == c || c->err != 0)
{
printf("RedisConnect failed: %s\n", c->errstr);
return 1;
}
printf("连接到Redis成功\n");
//执行Redis命令
//reply = redisCommand(c, "set var wuyou");
//reply = redisCommand(c, "set var %s", "hello wuyou");
//reply = redisCommand(c, "set %s %s", "var", "hello wuyou");
//执行set bar "hello" 后面的数字表示前面key和value的字节数
reply = redisCommand(c, "set %b %b", "bar", (size_t)3, "hello", (size_t)5);
if (NULL == reply)
{
printf("执行命令失败....\n");
return 1;
}
if (reply->type == REDIS_REPLY_STATUS)
{
printf("执行结果: %s\n", reply->str);
}
//断开连接和释放内存
redisFree(c);
return 0;
}
Redis执行incr命令
c
int main()
{
/*前面都要先保证连接数据库成功*/
//执行set bar "hello" 后面的数字表示前面key和value的字节数
reply = redisCommand(c, "incr num");
if (NULL == reply)
{
printf("执行命令失败....\n");
return 1;
}
if (reply->type == REDIS_REPLY_INTEGER)
{
printf("执行结果: %lld\n", reply->integer);
}
//断开连接和释放内存
redisFree(c);
return 0;
}
Redis执行lpush和lrem命令
c
int main()
{
//删除
reply = redisCommand(c, "del list");
if (NULL == reply)
{
printf("执行命令失败....\n");
return 1;
}
if (reply->type == REDIS_REPLY_INTEGER)
{
printf("执行结果: %lld\n", reply->integer);
}
//释放响应对象
freeReplyObject(reply);
//lpush命令
for (int i = 0; i < 10; i++)
{
char buf[32];
snprintf(buf, 64, "lpush list %u", i);
reply = redisCommand(c, buf);
//释放响应对象
freeReplyObject(reply);
}
//执行lrange命令
reply = redisCommand(c, "lrange list 0 -1");
if (reply->type == REDIS_REPLY_ARRAY)
{
for (int i = 0; i < reply->elements; i++)
{
printf("%u) %s\n", i, reply->element[i]->str);
}
}
//释放响应对象
freeReplyObject(reply);
//断开连接和释放内存
redisFree(c);
return 0;
}