Hiredis 基本使用

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 0. 前言  Hiredis是一个Redis的C客户端库函数,基本实现了Redis的协议的最小集。这里对hiredis的api作基本的介绍以及应用,主要参考hiredis的README文件以及相关源码。


0. 前言

  Hiredis是一个Redis的C客户端库函数,基本实现了Redis的协议的最小集。这里对hiredis的api作基本的介绍以及应用,主要参考hiredis的README文件以及相关源码。

1. 同步API

redisContext,该库的上下文环境。

 1 /* Context for a connection to Redis */
 2 typedef struct redisContext {
 3     int err; /* Error flags, 0 when there is no error */
 4     char errstr[128]; /* String representation of error when applicable */
 5     int fd;
 6     int flags;
 7     char *obuf; /* Write buffer */
 8     redisReader *reader; /* Protocol reader */
 9 
10     enum redisConnectionType connection_type;
11     struct timeval *timeout;
12 
13     struct {
14         char *host;
15         char *source_addr;
16         int port;
17     } tcp;
18 
19     struct {
20         char *path;
21     } unix_sock;
22 
23 } redisContext;

a. 连接Redis

1 //连接redis,若出错redisContext.err会设置为1,redisContext.errstr会包含描述错误信息
2 redisContext *redisConnect(const char *ip, int port);

 

b. 同步执行Redis命令

1 /*
2 
3 同步执行redis命令,和printf类似,%b传入二进制数据,要求有size_t参数指定长度。例如redisCommmand( c, "SET foo %b", arr, (size_t)len );
4 失败:返回NULL,并且err字段会置1,一旦执行出错,该redisContext就不能再使用,需要重新连接
5 成功:返回redisReply的指针
6 
7 */
8 void *redisCommand(redisContext *c, const char *format, ...);
 1 /*
 2 发送一组命令,通过argv以及argvlen指定,当argvlen为NULL时,argv每个字符串的长度通过strlen来计算,所以当需要传输二进制数据时,整个argvlen数据都要输入。
 3 */
 4 void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
 5 
 6 /*
 7     Piplining,追加的命令后可以通过redisGetReply来获取命令的返回值。
 8 */
 9 void redisAppendCommand(redisContext *c, const char *format, ...);
10 void redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
11 
12 //获取命令返回值,注:使用freeReplyObject释放
13 int redisGetReply(redisContext *c, void **reply);

 

c. 响应的数据结构

typedef struct redisReply {
  int type; /* REDIS_REPLY_* */ //指明返回的类型
  long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
  int 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返回数据的类型,redisReply.type字段

1 #define REDIS_REPLY_STRING 1 //字符串
2 #define REDIS_REPLY_ARRAY 2    //数组,多个reply,通过element数组以及elements数组大小访问
3 #define REDIS_REPLY_INTEGER 3    //整型, integer字段
4 #define REDIS_REPLY_NIL 4    //空,没有数据
5 #define REDIS_REPLY_STATUS 5    //状态,str字符串以及len
6 #define REDIS_REPLY_ERROR 6    //错误,同STATUS

 

d. 释放资源

1 //释放reply结构
2 void freeReplyObject(void *reply);
//关闭连接并释放内存
void redisFree(redisContext *c);

 

e. 错误字段说明 redisContext.err

1 //错误字段,会设置为以下其中一个值
2 #define REDIS_ERR_IO 1 /* Error in read or write */
3 #define REDIS_ERR_EOF 3 /* End of file */
4 #define REDIS_ERR_PROTOCOL 4 /* Protocol error */
5 #define REDIS_ERR_OOM 5 /* Out of memory */
6 #define REDIS_ERR_OTHER 2 /* Everything else... */

 

2. 异步API

   待补充

 

3. 同步API示例(实现订阅功能)

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "hiredis.h"
 4 
 5 void ProcessReply( redisReply * pReply )
 6 {
 7     redisReply * pSubReply = NULL;
 8 
 9     if ( pReply != NULL && pReply->elements == 3 )
10     {
11         pSubReply = pReply->element[2];
12         printf( "Msg [%s]\n", pSubReply->str );
13     }
14 }
15 
16 int main( int argc, char const *argv[] )
17 {
18     redisContext * pContext = redisConnect( "127.0.0.1", 6379 );
19 
20     if ( NULL == pContext || pContext->err == 1 )
21     {
22         printf( "%s\n", pContext->errstr );
23         exit( -1 );
24     }
25 
26     char * pKey = "DATABUS:REQ";
27 
28     redisReply * pReply = redisCommand( pContext, "SUBSCRIBE %s", pKey );
29     freeReplyObject( pReply );
30     while ( redisGetReply( pContext, (void **)&pReply ) == REDIS_OK )
31     {
32         ProcessReply( pReply );
33         freeReplyObject( pReply );
34     }
35 
36     redisFree( pContext );
37 
38     return 0;
39 }
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
3月前
|
存储 编译器 C++
|
4月前
Nestjs(二)cli 创建项目与基本使用
Nestjs(二)cli 创建项目与基本使用
69 5
|
3月前
|
存储 缓存 Java
【scoop】安装及基本使用
【scoop】安装及基本使用
170 0
|
10月前
|
JSON 编解码 JavaScript
hiredis和rapidjson库的使用小结
hiredis和rapidjson库的使用小结
|
安全 PHP
composer的基本使用和常见报错解决
composer的基本使用和常见报错解决
322 0
|
监控 Ubuntu Linux
Multitail 安装和基本使用
Multitail 安装和基本使用
321 0
|
NoSQL Linux API
Linux qtcreator编程使用redis客户端hiredis
Linux qtcreator编程使用redis客户端hiredis
586 0
|
存储 JSON PHP
Composer 基本使用
基本用法 引言 介绍基本用法,我们将安装 monolog/monolog 日志库作为范例。如果你还没有安装 Composer,请参阅 Composer 安装 章节。 注意:为简便起见,我们假定你已经 本地 安装了 Composer。
1345 0
|
Unix Linux C语言
MakeFile基本使用
MakeFile Making makefile demo # Run this line when useing `make` command # default is the target which is an output id in this makefile # name after `...
909 0
|
NoSQL API Redis