0. 前言

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

1. 同步API

redisContext,该库的上下文环境。

 /* Context for a connection to Redis */
typedef struct redisContext {
int err; /* Error flags, 0 when there is no error */
char errstr[]; /* String representation of error when applicable */
int fd;
int flags;
char *obuf; /* Write buffer */
redisReader *reader; /* Protocol reader */ enum redisConnectionType connection_type;
struct timeval *timeout; struct {
char *host;
char *source_addr;
int port;
} tcp; struct {
char *path;
} unix_sock; } redisContext;

a. 连接Redis

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

b. 同步执行Redis命令

 /*

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

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

d. 释放资源

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

e. 错误字段说明 redisContext.err

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

2. 异步API

  待补充

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

 #include <stdio.h>
#include <stdlib.h>
#include "hiredis.h" void ProcessReply( redisReply * pReply )
{
redisReply * pSubReply = NULL; if ( pReply != NULL && pReply->elements == )
{
pSubReply = pReply->element[];
printf( "Msg [%s]\n", pSubReply->str );
}
} int main( int argc, char const *argv[] )
{
redisContext * pContext = redisConnect( "127.0.0.1", ); if ( NULL == pContext || pContext->err == )
{
printf( "%s\n", pContext->errstr );
exit( - );
} char * pKey = "DATABUS:REQ"; redisReply * pReply = redisCommand( pContext, "SUBSCRIBE %s", pKey );
freeReplyObject( pReply );
while ( redisGetReply( pContext, (void **)&pReply ) == REDIS_OK )
{
ProcessReply( pReply );
freeReplyObject( pReply );
} redisFree( pContext ); return ;
}

运行结果如下:

Hiredis 基本使用的更多相关文章

  1. 萌新笔记——封装hiredis——C++与redis对接(一)(string的SET与GET操作)

    在菜鸟教程自学了redis,总想着像Mysql一样,在C/C++中进行对接.于是查询了一些资料,最后找到了hiredis.然而直接用它的话,难免有点不方便.于是,对其进行封装. hiredis直接去g ...

  2. 异步导入导出Redis数据(利用Hiredis、Libevent)

    最近工作中需要用到一个将数据从Redis导出到文本(或从文本导入Redis)的工具.找到一个用Ruby写的开源软件redis-dump(http://delanotes.com/redis-dump/ ...

  3. redis C接口hiredis 简单函数使用介绍

    hiredis是redis数据库的C接口,目前只能在linux下使用,几个基本的函数就可以操作redis数据库了. 函数原型:redisContext *redisConnect(const char ...

  4. 使用hiredis实现pipeline方式访问

    1.介绍 hiredis: 一个c/c++的访问redis的api库 地址:https://github.com/redis/hiredis pipeline方式: redis中的pipeline方式 ...

  5. hiredis的安装

    Hiredis客户端下载地址:https://github.com/antirez/hiredis/zipball/master Hiredis安装步骤: tar zxvf antirez-hired ...

  6. redis内存数据库C客户端hiredis API 中文说明

    A)编译安装 make make install (/usr/local) make install PREFIX=$HOME/progs(可以自由指定安装路径) B)同步的API接口 redisCo ...

  7. hiredis异步接口封装并导出到Lua

    hiredis异步接口封装并导出到Lua(金庆的专栏 2017.1)hiredis 不支持 Windows, Windows 下使用 wasppdotorg / hiredis-for-windows ...

  8. hiredis的各种windows版本

    hiredis的各种windows版本(金庆的专栏 2016.12)hiredis 是内存数据库 redis 的客户端C库, 不支持Windows.hiredis的Windows移植版本有许多:des ...

  9. c/c++(hiredis)异步调用redis【转】

    hiredis是redis官方推荐的C/C++客户端代码库.使用hiredis库很简易方便的进行redis的相关开发. 同步方式 不过大多数情况下,我们采用的都是同步的调用方式.   1 2 3 4 ...

随机推荐

  1. 51nod 1060反素数

    经典题. #include<map> #include<queue> #include<stack> #include<cmath> #include& ...

  2. 100114C

    打表找规律 第25项开始循环 #include<iostream> #include<cstdio> #include<algorithm> using names ...

  3. 关于selenium 3.0 + python 3.5中多层框架或窗口的定位driver.switch_to_frame()

    针对selenium3 中的窗口定位会自动划掉,不起作用 现在换成 driver.switch_to.frame()就会不报错了

  4. ActiveMQ;RabbitMQ;ZeroMQ

    中间件类型: Embedded middleware: As the name suggests, this typeof middleware handles embedded applicatio ...

  5. IE中使用jquery的fadeIn()失效的问题

    在自己写的一个轮播组件中遇到一个问题,使用jquery的fadeIn动画时,在IE11中表现不正常,没有渐入的效果. 1.HTML结构 <div class="mainpage-sli ...

  6. Leetcode 160. Intersection of two linked lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. kali2016.2源

    最近中科大的源出了问题,官方源又会重定向到意大利. 一下源目前亲测可用:kali2016.2源 清华大学 deb http://mirrors.tuna.tsinghua.edu.cn/kali ka ...

  8. 【bzoj2242】 SDOI2011—计算器

    http://www.lydsy.com/JudgeOnline/problem.php?id=2242 (题目链接) 题意 给出y,z,p.求:1.yz mod p:2.xy=z(mod p):3. ...

  9. bzoj1042: [HAOI2008]硬币购物

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  10. SQLServer2012自增列值跳跃的问题

    2012引入的新特性,重启之后会出现值跳跃的问题,如: 解决的方案: 1.使用序列(Sequence),2012引入的和Oracle一样的特性. 2.更改SQLServer启动服务的启动参数,增加[- ...