[![Build Status](https://github.com/redis/hiredis/actions/workflows/build.yml/badge.svg)](https://github.com/redis/hiredis/actions/workflows/build.yml)
**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](https://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.2`
<span style="color:red">NOTE: v1.0.1 erroneously bumped SONAME, which is why it is skipped here.</span>
Version 1.0.2 is simply 1.0.0 with a fix for [CVE-2021-32765](https://github.com/redis/hiredis/security/advisories/GHSA-hfm9-39pp-55p2). They are otherwise identical.
## 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 equivalent 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 ar
redis-7.0.9Linux安装包
需积分: 0 40 浏览量
更新于2023-11-12
收藏 2.85MB GZ 举报
Redis是一款高性能的键值对数据库,常用于缓存、消息队列等场景。在Linux系统中,安装Redis 7.0.9版本的过程相对简单,主要涉及到下载、解压、配置、编译以及启动服务等多个步骤。以下是详细的安装教程:
你需要确保你的Linux系统已经安装了必要的依赖软件。这些通常包括GCC编译器、make工具和jemalloc库。你可以通过运行以下命令检查并安装它们:
```bash
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libjemalloc-dev
```
如果你使用的是CentOS或RHEL,命令会有所不同:
```bash
sudo yum update
sudo yum install gcc make
sudo yum install jemalloc-devel
```
接下来,下载Redis 7.0.9的源代码压缩包。你可以通过访问Redis官方网站或者使用wget命令从互联网上获取:
```bash
wget http://download.redis.io/releases/redis-7.0.9.tar.gz
```
下载完成后,使用`tar`命令解压文件:
```bash
tar -zxvf redis-7.0.9.tar.gz
```
解压后的目录名为`redis-7.0.9`,进入该目录:
```bash
cd redis-7.0.9
```
现在,你可以开始编译和安装Redis。执行以下命令:
```bash
make
sudo make install
```
编译完成后,Redis的可执行文件会被安装到系统的`/usr/local/bin`目录下。此时,你可以通过`redis-server`命令启动Redis服务:
```bash
redis-server
```
但是,这样启动的Redis服务器会在当前终端上保持运行,并且当你关闭终端时,Redis也会停止。为了使Redis作为后台服务持续运行,可以创建一个配置文件,例如`/etc/redis/redis.conf`,然后指定该文件启动Redis:
```bash
sudo cp redis.conf /etc/redis/redis.conf
sudo vi /etc/redis/redis.conf
```
在`redis.conf`文件中,你可以配置各种选项,如绑定IP、端口、数据持久化等。保存并关闭文件后,通过以下命令启动Redis:
```bash
redis-server /etc/redis/redis.conf
```
为了方便管理,你还可以创建系统服务单元文件,使其能够用`systemctl`命令进行启动、停止和查看状态。创建一个名为`redis.service`的文件,放置在`/etc/systemd/system`目录下,内容如下:
```ini
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
Restart=always
[Install]
WantedBy=multi-user.target
```
创建完服务单元文件后,执行以下命令使改动生效:
```bash
sudo systemctl daemon-reload
sudo systemctl start redis
sudo systemctl enable redis
```
至此,Redis 7.0.9已经在Linux上成功安装并启动。你可以通过`redis-cli`命令进行客户端连接,测试其功能:
```bash
redis-cli
```
在Redis客户端中,你可以执行各种命令,如`SET key value`、`GET key`等。
注意,为了保证Redis的数据安全,你可能还需要配置相应的防火墙规则,允许特定的IP地址访问Redis服务。同时,考虑使用`requirepass`配置项设置密码,以增加服务的安全性。
以上就是安装Redis 7.0.9在Linux系统上的详细过程,希望对你有所帮助。在实际使用中,你可能还需要根据自己的需求进行进一步的优化和配置。