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 ...
随机推荐
- bzoj1834
第一问很好搞.第二问事实上可以这么想.如果一条边的流量还有,那么我们走过去不要钱,否则要钱,于是跑个费用流,就好了 (其实跑k次spfa也可以,我是这么写的) #include<cstdio&g ...
- mnsday2t1
枚举每个数的因子,然后该因子数量+1,最后扫描一遍,如果该因子数量小于等于m且该因子在1-n之间就输出 复杂度:枚举因子:O(n^1/2*m) 输出答案 : 大概是O(m*?) 一个不知道的数字 #i ...
- Mysql 5.7.12解压版的安装及配置系统编码
这篇博文是由于上篇EF+MySql博文引发的,上篇博文中在Seed方法中插入中文数据到Mysql数据库中乱码,后来网上找了N种方法也没解决.重装了MySql并在安装过程中配置了系统编码,此篇记录一下. ...
- MAC OS VPN使用指南
L2TP/IPSec VPN - Mac OS 使用指南 一.打开Mac的联网设置. 二.然后点左边的加号,添加新的连接. 三.选择VPN(L2TP),点击创建(Create). 四.输入你购买的VP ...
- iOS清理缓存的几种方法
- (long long) fileSizeAtPath:(NSString*) filePath{ NSFileManager* manager = [NSFileManager defau ...
- CodeForces 51F Caterpillar
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- CVE-2014-0050: Exploit with Boundaries, Loops without Boundaries、Apache Commons FileUpload and Apache Tomcat DoS
catalog . Description . Analysis . POC . Solution 1. Description MultipartStream.java in Apache Comm ...
- C#6.0特性笔记
Visual Studio 2015的C#6.0,今天无意中看这个视频,怕忘记其中的特性,故此进行记录. public class Point { //Getter专属自动属性 public int ...
- STL之vector
参考资料: 1.codeproject 2.csdn 3.cplusplus
- MyBatis详解 与配置MyBatis+Spring+MySql
MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的XML 和注解来配置和映射基本 ...