# 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 an 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
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:
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 an 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:
redisContext *c = redisConnect("127.0.0.1", 6379);
if (c->err) {
printf("Error: %s\n", c->errstr);
// handle error
}
### 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:
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:
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:
reply = redisCommand(context, "SET foo %b", value, 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:
reply = redisCommand("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:
* **`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.
Replies should be freed using the `freeReplyObject()` function.
Note that this function will take care of freeing sub-replies objects
contained in arrays and nested arrays, so there is no need for the user to
free the sub replies (it is actually harmful and will corrupt the memory).
### Cleaning up
To disconnect and free the context the following function can be used:
void redisFree(redisContext *c);
This function immediately closes the socket and then free's the allocations done in
creating the context.
### Sending commands (cont'd)
Together with `redisCommand`, the function `redisCommandArgv` can be used to issue commands.
It has the following prototype:
void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
It takes the number of arguments `argc`, an array of strings `argv` and the lengths of the
arguments `argvlen`. For convenience, `argvlen` may be set to `NULL` and the function will
use `strlen(3)` on every argument to determine its length. Obviously, when any of the arguments
need to be binary safe, the entire array of lengths `argvlen` should be provided.
The return value has the same semantic as `redisCommand`.
### Pipelining
To explain how Hiredis supports pipelining in a blocking connection, there needs to be
understanding of the internal execution flow.
When any of the functions in the `redisCommand` family is called, Hiredis first formats the
command according to the Redis protocol. The formatted command is then put in the output buffer
of the context. This output buffer is dynamic, so it can hold any number of commands.
After the command is put in the output buffer, `redisGetReply` is called. This function has the
following two execution paths:
1. The input buffer is non-empty:
* Try to parse a single reply from the input buffer and return it
* If no reply could be parsed, continue at *2*
2. The input buffer is empty:
* Write the **entire** output buffer to the socket
* Read from the socket until a single reply could be parsed
The function `redisGetReply` is exported as part of the Hiredis API and can be used when a reply
is expected on the socket. To pipeline commands, the only things that needs to be done is
filling up the output buffer. For this cause, two commands can be used that are identical
to the `redisCommand` family, apart from not returning a reply:
void redisAppendCommand(redisContext *c, const char *format, ...);
void redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
After calling either function one or more times, `redisGetReply` can be used to receive the
subsequent replies. The return value for this function is either `REDIS_OK` or `REDIS_ERR`, where
the latter means an error occurred while reading a reply. Just as with the other commands,
the `err` field in the context can be used to find out what the cause of this error is.
The following examples shows a simple pipeline (resulting in only a single call to `write(
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
https://github.com/MSOpenTech/redis就是这个的,不过这里下下来的不能用,我自己搞了好久才搞了一个可以用的,主要是用里面的hiredis; 你也可以自己做一个:http://www.cnblogs.com/sixbeauty/p/3954199.html
资源推荐
资源详情
资源评论
收起资源包目录
windows下的redis源码,vs2010版 (1235个子文件)
00-RELEASENOTES 5KB
0061583f42d61fa905d711e408a8d2dee96792 82KB
018035ec369820362929584143fb0713ed9673 1KB
019e866114b0a3df4d35796ab318e02db81a7d 260B
02d086866ed8440bc2e6143f1e4e7f8e8f5afc 2KB
0301d5b4c537735dea37939b1fa866fb83d64c 2KB
03718a8f4dde2315d4b32294d5343ca6731df0 7KB
038f14aca5172473fe8e5b31a878586785a1eb 19KB
043274e9925dceda4679a1404a05313585435f 605B
06759e09e47afddb1a085dd84831636c5367b4 1KB
078fe36370701474ffff4ae3085908f1f292e1 2KB
07f3dbbb729ed7459df7c76833e10f7ad8daa4 246B
0807f9ed6e3b78a158bdd647763c465948c9ef 59B
082ddb5d421dcb844ae4185cbad066801879fc 5KB
08459eac715c0456673b77b70ee1ce65e1f2e6 788B
08839ee481e6293019ff4b711f3ce351d5e41c 3KB
08879510fa6619262225b84896d7f1ab533fec 3KB
0993c50809a02be0f9f3d74b6330f4e1cb294e 3KB
099e79af194628373a334c2fec4c1279a3ff2f 14KB
09f1eedb17a6359b46bef143df5c1af77e77a6 4KB
0a692bf9273b754b77101242f711cd75e0c2bc 2KB
0c0eb3f619064eb4fe5f38694089cbfcc40706 58B
0c347ecd54936398140e8e22752df19d27d531 182B
0c709ec748e29e5d22d87303cbb981051465dc 664B
0d922c81c15545aa55aa2385ea5ba0c6e76d7b 4KB
0df6ba299789e4f7b2984b1503d1b28744a81f 5KB
100aa9515dd4e9aa4225f30291e443135a49e3 2KB
101e562f1f7e00dbf0b040373ed2d0a942bf4d 4KB
1022a76f533847fac76852af1b350185c2da96 899B
109596c0f4b5ddd43d171fc3e94f2b6f6b10ee 1KB
10cb854032038dbf4e77de86b26942c9984b94 58B
11315225e8c9d108e95bbdfbcbde5f2f08ea88 1KB
1136257488cfc3d439bd930e49d8a7bd26135a 2KB
11beb9b4c3ff410b5f2526bfd5743095e16df5 1KB
12263039da4be39ffc80e7c8e4d413520ece60 23KB
12519ac0cd013d7696583ba37bae4994ca6bb9 1KB
129b5c956b21014e1663cafd2764ae6a0368ab 3KB
130b506c55516ad8d49b898596b0e366f59186 938B
137a1828e0f226879b87f612a9994f0ca1ef63 1KB
13a941c5179561ca8241c17f4e1f67f796a30c 502B
14fb4190de1adc19d895d7e452774e2de9178c 2KB
151f5348a6f181b8330be0083f3ea728db2449 2KB
158ff87656983b7b89161087d7811dda36a6ca 1KB
1608be5b85519b9e64ab8232f4984646ed10f4 1KB
16b673c61df2bffe86ed19f2853be350dd6e4b 219B
172c0640ce53a24b5b7486c94ce52cbf7f4ff3 1KB
1754410cd160e3e125781bc80f41405c09d97c 2KB
17a7ebd7f4331b09a5fff5dc5b40dd35c1d778 8KB
180cd41d36dd32225e30912fe565360aa7c7ff 2KB
1839f7f920d205d42d39f26ff3560e240cd970 3KB
1847f92c08658a9ff0fbd5a2b5b53272a33a39 828B
18662601767edc2306bc247b51712415ad08c4 3KB
187da5f5d9e241ed0f9bef6fa2b3305658d462 11KB
18b9f441ccec82e5bbd036a7c0df02f12fe86f 8KB
195c5db4b2f8e65de6e55603b9752f3729b24b 1KB
19f86d49d0a44d117ad51cfcbd8b5bc03de3be 1KB
1add58c6077dce547013c7d9a24ff19ba74524 1KB
1d0cf281c166b157d703fbbcbcd109e0a0b82e 203B
1d78c041bac07a0e7ea2646cddc3426457395d 1KB
1d83afa4f2b7a2d41272d964c440f5065da32c 6KB
1e7880bfac42b525dfe3cb126c2b823b23453b 2KB
200d4b43309a241510801f1098a13396bf96f6 4KB
2079abda668e5fc8f8de6f226e7b26384e8a03 15KB
21361bc70cca68f420176011df6f648d3bb17b 1KB
22dde17f5ee2046d9083de1aec8b0b9438f664 2KB
235a51bc744282123a1e0b1c3b2aa746b62379 2KB
23a0f3d10929de21f6aaf8d8400c4d5e501565 606B
23c5ae926b58b1e7350a55d0b2e28c3cb30db2 1KB
274daeef5d67526856f6282ab505e59901cf2d 1KB
281af632445727dd03ddcd18316d5df2c63879 829B
2851276a6184b7af5d741a6b926bd476d19165 14KB
2a245985e02d6d4eda57aced778171e3cd8f50 4KB
2b38809e9e20d49ff92b25946850ae8b9c0238 2KB
2c14d1e95815736aae8cb1478ac2f6e123da36 1KB
2ca29ae643e7843c36d1b643771d29d3791a55 3KB
2d7a1e37edf50b9686e7ddbe0c5c7709c5d9c9 9KB
2e85d2bee9c5ecf027e3b36b106a48ef605741 498B
ev.3 248KB
eio.3 157KB
jemalloc.3 51KB
31b47cfbb126edda4249ac1508691f2010334d 1KB
31cea6fb79b42edda1a4dd4ab9745fb783c237 409B
32d47c9a833bffbcd392cbc239535771082bce 58B
3326692b143398ffdf9df9881fd96aaaa4e5a9 199B
33f54a7c0798eeb4215c6cc35fce9e99840c0a 4KB
354eecba023f09efb4d75f8f8ac14cf0627384 2KB
3622e72452c1d77a4cfb23cfbbf86ded26a772 2KB
391a9ef2ec7fb4c6eb6ec11aa459d56393eebb 901B
3ae0de93c6055e2346e1a5d02afab295ea3e20 2KB
3aee3ffbdc9bd799a7781f383cc6e698f268a1 2KB
3b179f280506224da315742e1d32f8ac421327 1KB
3b4ce4d8ede652edfc430e238bed81983fbbd0 2KB
3b8a8830739736b32677b10a55b8424fa07802 1KB
3c729a9dc5c920f129fc61b3f81e30356e6d14 518B
3ce47670127358bbf5fbfe23e364613ebb5925 2KB
3da732bac28eb059f50173e1dc7920bb3a3ba1 6KB
3e3231c12c0f0d2bbcb4ae3bc93aa47164beba 2KB
3f13ff223216f263dd8e84434e629862d685bc 2KB
403c6f470494320d5de96f75f16f99946c68ac 1KB
41b563f4a44529a3da7c8598c14fa5e518d545 885B
共 1235 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
陈小润
- 粉丝: 2
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 技术资料分享基于JPEG标准的图像处理及其在MCF5329上的实现很好的技术资料.zip
- 技术资料分享基于FPGA的JPEG解码算法的研究与实现很好的技术资料.zip
- 技术资料分享基于ENC28J60以太网控制器及其应用很好的技术资料.zip
- 技术资料分享基于ENC28J60的嵌入式网络接口的设计很好的技术资料.zip
- 技术资料分享基于ARM的嵌入式静态图像显示系统的研究与实现很好的技术资料.zip
- 技术资料分享关于STM32的IAP总结很好的技术资料.zip
- 技术资料分享高通CAMIF和Ov-sensor-调试总结很好的技术资料.zip
- 技术资料分享二阶RC滤波试验很好的技术资料.zip
- 技术资料分享多核处理器构架的高速JPEG解码算法很好的技术资料.zip
- 技术资料分享第24章 性能和资源占用很好的技术资料.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页