hiredis/README.md

117 lines
3.9 KiB
Markdown
Raw Normal View History

2010-05-18 16:49:16 +00:00
HIREDIS OVERVIEW
----------------
Hiredis is a minimalistic C client library for the Redis 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.
2010-10-11 22:31:09 +00:00
Hiredis only supports the new Redis protocol, so you can use it with any
2010-05-18 16:49:16 +00:00
Redis version >= 1.2.0.
HIREDIS API
-----------
Hiredis exports only three function calls:
2010-10-11 22:31:09 +00:00
redisReply *redisConnect(int *fd, char *ip, int port);
redisReply *redisCommand(int fd, char *format, ...);
void freeReplyObject(redisReply *r);
2010-05-18 16:49:16 +00:00
The first function is used in order to create a connection to the Redis server:
redisReply *reply;
int fd;
reply = redisConnect(&fd,"127.0.0.1",6379);
to test for connection errors all it is needed to do is checking if reply
is not NULL:
if (reply != NULL) {
printf("Connection error: %s\n", reply->reply);
freeReplyObject(reply);
exit(1);
}
When a reply object returns an error, the reply->type is set to the value
2010-10-11 22:31:09 +00:00
`REDIS_REPLY_ERROR`, and reply->reply points to a C string with the description
2010-05-18 16:49:16 +00:00
of the error.
2010-10-11 22:31:09 +00:00
In the above example we don't check for reply->type as `redisConnect()` can
only return `NULL` or a reply object that is actually an error.
2010-05-18 16:49:16 +00:00
2010-10-11 22:31:09 +00:00
As you can see `redisConnect()` will just set (by reference) the `fd` variable
2010-05-18 16:49:16 +00:00
to the file descriptor of the open socket connected to our Redis server.
2010-10-11 22:31:09 +00:00
Calls to `redisCommand()` will require this file descriptor as first argument.
2010-05-18 16:49:16 +00:00
2010-10-11 22:31:09 +00:00
SENDING COMMANDS
2010-05-18 16:49:16 +00:00
----------------
Commands are sent using a printf-alike format. In the simplest form it is
like that:
reply = redisCommand("SET foo bar");
But you can use "%s" and "%b" format specifiers to create commands in a
printf-alike fashion:
reply = redisComand("SET foo %s", somevalue);
2010-10-11 22:31:09 +00:00
If your arguments are binary safe, you can use "%b" that receives the pointer
2010-05-18 16:49:16 +00:00
to the buffer and a size_t integer with the length of the buffer.
reply = redisCommand("SET %s %b", "foo", somevalue, somevalue_length);
Internally Hiredis will split the command in different arguments and will
convert it to the actual protocol used to communicate with Redis.
Every space will separate arguments, so you can use interpolation.
The following example is valid:
reply = redisCommand("SET key:%s %s", myid, value);
USING REPLIES
-------------
2010-10-11 22:31:09 +00:00
`redisCommand()` returns a reply object. In order to use this object you
2010-05-18 16:49:16 +00:00
need to test the reply->type field, that can be one of the following types:
2010-10-11 22:31:09 +00:00
* `REDIS_REPLY_ERROR`:
2010-05-18 16:49:16 +00:00
The command returned an error string, that can be read accessing to
the reply->reply field.
2010-10-11 22:31:09 +00:00
* `REDIS_REPLY_STRING`:
2010-05-18 16:49:16 +00:00
The command returned a string, that can be read accessing to the
2010-10-11 22:31:09 +00:00
reply->reply field. The string is always null-terminated, but when you
2010-05-18 16:49:16 +00:00
need to work with binary-safe strings you can obtain the exact length
2010-10-11 22:31:09 +00:00
of the reply with: `sdslen(reply->reply)`.
2010-05-18 16:49:16 +00:00
2010-10-11 22:31:09 +00:00
* `REDIS_REPLY_ARRAY`:
2010-05-18 16:49:16 +00:00
The command returned an array of reply->elements elements.
Every element is a redisReply object, stored at redis->element[..index..]
2010-10-11 22:31:09 +00:00
Redis may reply with nested arrays but this is fully supported.
2010-05-18 16:49:16 +00:00
2010-10-11 22:31:09 +00:00
* `REDIS_REPLY_INTEGER`:
2010-05-18 16:49:16 +00:00
The command replies with an integer. It's possible to access this integer
using the reply->integer field that is of type "long long".
2010-10-11 22:31:09 +00:00
* `REDIS_REPLY_NIL`:
2010-05-18 16:49:16 +00:00
The command replies with a NIL special object. There is no data to access.
FREEING REPLIES
---------------
2010-10-11 22:31:09 +00:00
Replies should be freed using the `freeReplyObject()` function.
2010-05-18 16:49:16 +00:00
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).
AUHTOR
------
Hiredis was written by Salvatore Sanfilippo (antirez at gmail) and is
released under the BSD license.