Hiredis 基本使用
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 基本使用的更多相关文章
- 萌新笔记——封装hiredis——C++与redis对接(一)(string的SET与GET操作)
在菜鸟教程自学了redis,总想着像Mysql一样,在C/C++中进行对接.于是查询了一些资料,最后找到了hiredis.然而直接用它的话,难免有点不方便.于是,对其进行封装. hiredis直接去g ...
- 异步导入导出Redis数据(利用Hiredis、Libevent)
最近工作中需要用到一个将数据从Redis导出到文本(或从文本导入Redis)的工具.找到一个用Ruby写的开源软件redis-dump(http://delanotes.com/redis-dump/ ...
- redis C接口hiredis 简单函数使用介绍
hiredis是redis数据库的C接口,目前只能在linux下使用,几个基本的函数就可以操作redis数据库了. 函数原型:redisContext *redisConnect(const char ...
- 使用hiredis实现pipeline方式访问
1.介绍 hiredis: 一个c/c++的访问redis的api库 地址:https://github.com/redis/hiredis pipeline方式: redis中的pipeline方式 ...
- hiredis的安装
Hiredis客户端下载地址:https://github.com/antirez/hiredis/zipball/master Hiredis安装步骤: tar zxvf antirez-hired ...
- redis内存数据库C客户端hiredis API 中文说明
A)编译安装 make make install (/usr/local) make install PREFIX=$HOME/progs(可以自由指定安装路径) B)同步的API接口 redisCo ...
- hiredis异步接口封装并导出到Lua
hiredis异步接口封装并导出到Lua(金庆的专栏 2017.1)hiredis 不支持 Windows, Windows 下使用 wasppdotorg / hiredis-for-windows ...
- hiredis的各种windows版本
hiredis的各种windows版本(金庆的专栏 2016.12)hiredis 是内存数据库 redis 的客户端C库, 不支持Windows.hiredis的Windows移植版本有许多:des ...
- c/c++(hiredis)异步调用redis【转】
hiredis是redis官方推荐的C/C++客户端代码库.使用hiredis库很简易方便的进行redis的相关开发. 同步方式 不过大多数情况下,我们采用的都是同步的调用方式. 1 2 3 4 ...
随机推荐
- ( 转 ) Android自绘字体大小paint.settextsize随分辨率大小变化
1.获取当前设备的屏幕大小 DisplayMetrics displayMetrics = new DisplayMetrics(); this.getWindowManager().getDefau ...
- 【CodeVS 2083】Cryptcowgraphy 解密牛语
http://codevs.cn/problem/2083/ 奶牛搜索题.我加了如下剪枝: 1.用字符串hash判重.注意判重时也要对字符串长度判重,否则会出现两个字符串长度不同但hash值相同的情况 ...
- 【URAL 1486】Equal Squares
题意:求给定字符矩阵中相同正方形矩阵的最大边长和这两个相同正方形的位置 第一次写字符串哈希,选两个不同的模数进行二维字符串哈希. 本来应该取模判断相等后再暴力扫矩阵来判断,但是我看到<Hash在 ...
- CMY/CMYK 打印机色彩
CMY 发光物体和反光物体产生颜色的机制不同. 前者指光源光, 它的颜色由发光波长决定. 后者指不能发光但却能表现出颜色的物体, 例如色素. 色素的颜色由它不能吸收的光的波长决定. 比如红色色素, 除 ...
- Kernel Methods - An conclusion
Kernel Methods理论的几个要点: 隐藏的特征映射函数\(\Phi\) 核函数\(\kappa\): 条件: 对称, 正半定; 合法的每个kernel function都能找到对应的\(\P ...
- HTTP协议学习---(十)拓展-HTTPS协议
HTTPS(Hypertext Transfer Protocol over Secure Socket Layer,基于SSL的HTTP协议)使用了HTTP协议,但HTTPS使用不同于HTTP协议的 ...
- spring spel
•Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. •语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpE ...
- Mac OS 下 eclipse中文乱码解决方法(eclipse for mac 中文乱码)
由于一些java源码是从其他人那里拷贝过来,放入Mac os 版本的eclipse下,发现中文都是乱码.经过小试,可以解决. 1.打开eclipse 偏好设置 2.General ——>Cont ...
- WebClient 使用
--post 请求 public static string PostMsg(Guid orgid, int page, int rows) { System.N ...
- [日常训练]常州集训day3
T1 Description 有$K$个石子,石子只能放在$N$条水平线与$M$条竖直线构成的网格的交点上. 求用$K$个石子最多能找到多少四边平行于坐标轴的长方形,它的四个角上都恰好放着一枚石子. ...