[![Build Status](https://travis-ci.org/redis/hiredis.png)](https://travis-ci.org/redis/hiredis)
**This Readme reflects the latest changed in the master branch. See [v1.0.0](https://github.com/redis/hiredis/tree/v1.0.0) for the Readme and documentation for the latest release ([API/ABI history](https://abi-laboratory.pro/?view=timeline&l=hiredis)).**
# HIREDIS
Hiredis is a minimalistic C client library for the [Redis](http://redis.io/) database.
It is minimalistic because it just adds minimal support for the protocol, but
at the same time it uses a high level printf-alike API in order to make it
much higher level than otherwise suggested by its minimal code base and the
lack of explicit bindings for every Redis command.
Apart from supporting sending commands and receiving replies, it comes with
a reply parser that is decoupled from the I/O layer. It
is a stream parser designed for easy reusability, which can for instance be used
in higher level language bindings for efficient reply parsing.
Hiredis only supports the binary-safe Redis protocol, so you can use it with any
Redis version >= 1.2.0.
The library comes with multiple APIs. There is the
*synchronous API*, the *asynchronous API* and the *reply parsing API*.
## Upgrading to `1.0.0`
Version 1.0.0 marks the first stable release of Hiredis.
It includes some minor breaking changes, mostly to make the exposed API more uniform and self-explanatory.
It also bundles the updated `sds` library, to sync up with upstream and Redis.
For code changes see the [Changelog](CHANGELOG.md).
_Note: As described below, a few member names have been changed but most applications should be able to upgrade with minor code changes and recompiling._
## IMPORTANT: Breaking changes from `0.14.1` -> `1.0.0`
* `redisContext` has two additional members (`free_privdata`, and `privctx`).
* `redisOptions.timeout` has been renamed to `redisOptions.connect_timeout`, and we've added `redisOptions.command_timeout`.
* `redisReplyObjectFunctions.createArray` now takes `size_t` instead of `int` for its length parameter.
## IMPORTANT: Breaking changes when upgrading from 0.13.x -> 0.14.x
Bulk and multi-bulk lengths less than -1 or greater than `LLONG_MAX` are now
protocol errors. This is consistent with the RESP specification. On 32-bit
platforms, the upper bound is lowered to `SIZE_MAX`.
Change `redisReply.len` to `size_t`, as it denotes the the size of a string
User code should compare this to `size_t` values as well. If it was used to
compare to other values, casting might be necessary or can be removed, if
casting was applied before.
## Upgrading from `<0.9.0`
Version 0.9.0 is a major overhaul of hiredis in every aspect. However, upgrading existing
code using hiredis should not be a big pain. The key thing to keep in mind when
upgrading is that hiredis >= 0.9.0 uses a `redisContext*` to keep state, in contrast to
the stateless 0.0.1 that only has a file descriptor to work with.
## Synchronous API
To consume the synchronous API, there are only a few function calls that need to be introduced:
```c
redisContext *redisConnect(const char *ip, int port);
void *redisCommand(redisContext *c, const char *format, ...);
void freeReplyObject(void *reply);
```
### Connecting
The function `redisConnect` is used to create a so-called `redisContext`. The
context is where Hiredis holds state for a connection. The `redisContext`
struct has an integer `err` field that is non-zero when the connection is in
an error state. The field `errstr` will contain a string with a description of
the error. More information on errors can be found in the **Errors** section.
After trying to connect to Redis using `redisConnect` you should
check the `err` field to see if establishing the connection was successful:
```c
redisContext *c = redisConnect("127.0.0.1", 6379);
if (c == NULL || c->err) {
if (c) {
printf("Error: %s\n", c->errstr);
// handle error
} else {
printf("Can't allocate redis context\n");
}
}
```
*Note: A `redisContext` is not thread-safe.*
### Sending commands
There are several ways to issue commands to Redis. The first that will be introduced is
`redisCommand`. This function takes a format similar to printf. In the simplest form,
it is used like this:
```c
reply = redisCommand(context, "SET foo bar");
```
The specifier `%s` interpolates a string in the command, and uses `strlen` to
determine the length of the string:
```c
reply = redisCommand(context, "SET foo %s", value);
```
When you need to pass binary safe strings in a command, the `%b` specifier can be
used. Together with a pointer to the string, it requires a `size_t` length argument
of the string:
```c
reply = redisCommand(context, "SET foo %b", value, (size_t) valuelen);
```
Internally, Hiredis splits the command in different arguments and will
convert it to the protocol used to communicate with Redis.
One or more spaces separates arguments, so you can use the specifiers
anywhere in an argument:
```c
reply = redisCommand(context, "SET key:%s %s", myid, value);
```
### Using replies
The return value of `redisCommand` holds a reply when the command was
successfully executed. When an error occurs, the return value is `NULL` and
the `err` field in the context will be set (see section on **Errors**).
Once an error is returned the context cannot be reused and you should set up
a new connection.
The standard replies that `redisCommand` are of the type `redisReply`. The
`type` field in the `redisReply` should be used to test what kind of reply
was received:
### RESP2
* **`REDIS_REPLY_STATUS`**:
* The command replied with a status reply. The status string can be accessed using `reply->str`.
The length of this string can be accessed using `reply->len`.
* **`REDIS_REPLY_ERROR`**:
* The command replied with an error. The error string can be accessed identical to `REDIS_REPLY_STATUS`.
* **`REDIS_REPLY_INTEGER`**:
* The command replied with an integer. The integer value can be accessed using the
`reply->integer` field of type `long long`.
* **`REDIS_REPLY_NIL`**:
* The command replied with a **nil** object. There is no data to access.
* **`REDIS_REPLY_STRING`**:
* A bulk (string) reply. The value of the reply can be accessed using `reply->str`.
The length of this string can be accessed using `reply->len`.
* **`REDIS_REPLY_ARRAY`**:
* A multi bulk reply. The number of elements in the multi bulk reply is stored in
`reply->elements`. Every element in the multi bulk reply is a `redisReply` object as well
and can be accessed via `reply->element[..index..]`.
Redis may reply with nested arrays but this is fully supported.
### RESP3
Hiredis also supports every new `RESP3` data type which are as follows. For more information about the protocol see the `RESP3` [specification.](https://github.com/antirez/RESP3/blob/master/spec.md)
* **`REDIS_REPLY_DOUBLE`**:
* The command replied with a double-precision floating point number.
The value is stored as a string in the `str` member, and can be converted with `strtod` or similar.
* **`REDIS_REPLY_BOOL`**:
* A boolean true/false reply.
The value is stored in the `integer` member and will be either `0` or `1`.
* **`REDIS_REPLY_MAP`**:
* An array with the added invariant that there will always be an even number of elements.
The MAP is functionally equivelant to `REDIS_REPLY_ARRAY` except for the previously mentioned invariant.
* **`REDIS_REPLY_SET`**:
* An array response where each entry is unique.
Like the MAP type, the data is identical to an array response except there are no duplicate values.
* **`REDIS_REPLY_PUSH`**:
* An array that can be generated spontaneously by Redis.
This array response will always contain at least two subelements. The first contains the type of `PUSH` message (e.g. `message`, or `invalidate`), and the second being a sub-array with the `PUSH` payload itself.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
redis6.0离线安装使用-centos7 (2000个子文件)
lua.1 4KB
luac.1 4KB
._luac.1 176B
._lua.1 176B
._adapters 176B
._bin 176B
._build-aux 176B
._C99 176B
._ChangeLog 176B
._configure 176B
._COPYING 176B
._COPYING 176B
._COPYRIGHT 176B
._cpp 176B
._doc 176B
._doc 176B
._etc 176B
._examples 176B
._hdr_histogram 176B
._hiredis 176B
._HISTORY 176B
._include 176B
._include 176B
._INSTALL 176B
._INSTALL 176B
._install-sh 176B
._integration 176B
._internal 176B
._ISSUE_TEMPLATE 176B
._jemalloc 176B
._jemalloc 176B
._jemalloc 176B
._jemalloc 176B
._jemalloc-config 176B
._jeprof 176B
._lib 176B
._linenoise 176B
._lua 176B
._lua 176B
._luac 176B
._m4 176B
._Makefile 176B
._Makefile 176B
._Makefile 176B
._Makefile 176B
._Makefile 176B
._Makefile 176B
._Makefile 176B
._Makefile 176B
._msvc 176B
._msvc_compat 176B
._projects 176B
._README 176B
._README 176B
._README 176B
._README 176B
._redis-server 176B
._runtest-cluster 176B
._runtest-moduleapi 176B
._scripts 176B
._src 176B
._src 176B
._src 176B
._src 176B
._stress 176B
._test 176B
._test 176B
._test 176B
._test_threads 176B
._test_threads 176B
._test_threads 176B
._tests 176B
._unit 176B
._vc2015 176B
._vc2017 176B
._VERSION 176B
._workflows 176B
libjemalloc.a 19.06MB
libhiredis.a 482KB
liblua.a 399KB
._libhiredis.a 176B
._libjemalloc.a 176B
._liblua.a 176B
configure.ac 71KB
._configure.ac 176B
private_symbols_jet.awk 1KB
private_symbols.awk 1KB
._private_symbols.awk 176B
._private_symbols_jet.awk 176B
luavs.bat 1KB
._luavs.bat 176B
module.c 375KB
redis-cli.c 311KB
cluster.c 237KB
server.c 236KB
sentinel.c 200KB
replication.c 151KB
networking.c 144KB
t_stream.c 142KB
t_zset.c 141KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
知道的越多越难受
- 粉丝: 390
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 大神asp.net随书光盘资源信息管理系统毕业课程源码设计
- IMG_1034.jpg
- 配置虚拟机可以让您在一个物理主机上运行多个独立的操作系统实例,每个实例都有自己的硬件资源(如CPU、内存、硬盘等) 这非常适合测
- PLC(Programmable Logic Controller,可编程逻辑控制器)是一种专为工业环境设计的数字运算操作电子系
- 基于超声图像的心脏分割数据集 (超声心动图左心室心内膜,左心室心外膜,左心房分割)
- 第十九届全国大学生智能汽车竞赛是在2024年举办的一项重要赛事,吸引了众多高校的学生参加 这项竞赛旨在推动智能汽车技术的发展,鼓
- Apache JMeter 是一款开源的负载测试工具,主要用于测试Web应用,但也可以用于测试其他类型的服务器,如FTP、数据库
- 如果你正在准备一份针对程序员的问卷调查,那么设计问卷时应该考虑到目标群体的特点以及你希望获得的具体信息 以下是一些可能包含的问题
- 基于Matlab界面GUI设计的身份证号码识别源码[Matlab界面GUI设计].zip
- 基于Matlab界面GUI设计的人体行为识别[Matlab界面GUI设计].zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功