# redis-rs
[![Rust](https://github.com/redis-rs/redis-rs/actions/workflows/rust.yml/badge.svg)](https://github.com/redis-rs/redis-rs/actions/workflows/rust.yml)
[![crates.io](https://img.shields.io/crates/v/redis.svg)](https://crates.io/crates/redis)
[![Chat](https://img.shields.io/discord/976380008299917365?logo=discord)](https://discord.gg/WHKcJK9AKP)
Redis-rs is a high level redis library for Rust. It provides convenient access
to all Redis functionality through a very flexible but low-level API. It
uses a customizable type conversion trait so that any operation can return
results in just the type you are expecting. This makes for a very pleasant
development experience.
The crate is called `redis` and you can depend on it via cargo:
```ini
[dependencies]
redis = "0.25.3"
```
Documentation on the library can be found at
[docs.rs/redis](https://docs.rs/redis).
**Note: redis-rs requires at least Rust 1.60.**
## Basic Operation
To open a connection you need to create a client and then to fetch a
connection from it. In the future there will be a connection pool for
those, currently each connection is separate and not pooled.
Many commands are implemented through the `Commands` trait but manual
command creation is also possible.
```rust
use redis::Commands;
fn fetch_an_integer() -> redis::RedisResult<isize> {
// connect to redis
let client = redis::Client::open("redis://127.0.0.1/")?;
let mut con = client.get_connection()?;
// throw away the result, just make sure it does not fail
let _ : () = con.set("my_key", 42)?;
// read back the key and return it. Because the return value
// from the function is a result for integer this will automatically
// convert into one.
con.get("my_key")
}
```
Variables are converted to and from the Redis format for a wide variety of types
(`String`, num types, tuples, `Vec<u8>`). If you want to use it with your own types,
you can implement the `FromRedisValue` and `ToRedisArgs` traits, or derive it with the
[redis-macros](https://github.com/daniel7grant/redis-macros/#json-wrapper-with-redisjson) crate.
## Async support
To enable asynchronous clients, enable the relevant feature in your Cargo.toml,
`tokio-comp` for tokio users or `async-std-comp` for async-std users.
```
# if you use tokio
redis = { version = "0.25.3", features = ["tokio-comp"] }
# if you use async-std
redis = { version = "0.25.3", features = ["async-std-comp"] }
```
## TLS Support
To enable TLS support, you need to use the relevant feature entry in your Cargo.toml.
Currently, `native-tls` and `rustls` are supported.
To use `native-tls`:
```
redis = { version = "0.25.3", features = ["tls-native-tls"] }
# if you use tokio
redis = { version = "0.25.3", features = ["tokio-native-tls-comp"] }
# if you use async-std
redis = { version = "0.25.3", features = ["async-std-native-tls-comp"] }
```
To use `rustls`:
```
redis = { version = "0.25.3", features = ["tls-rustls"] }
# if you use tokio
redis = { version = "0.25.3", features = ["tokio-rustls-comp"] }
# if you use async-std
redis = { version = "0.25.3", features = ["async-std-rustls-comp"] }
```
With `rustls`, you can add the following feature flags on top of other feature flags to enable additional features:
- `tls-rustls-insecure`: Allow insecure TLS connections
- `tls-rustls-webpki-roots`: Use `webpki-roots` (Mozilla's root certificates) instead of native root certificates
then you should be able to connect to a redis instance using the `rediss://` URL scheme:
```rust
let client = redis::Client::open("rediss://127.0.0.1/")?;
```
To enable insecure mode, append `#insecure` at the end of the URL:
```rust
let client = redis::Client::open("rediss://127.0.0.1/#insecure")?;
```
**Deprecation Notice:** If you were using the `tls` or `async-std-tls-comp` features, please use the `tls-native-tls` or `async-std-native-tls-comp` features respectively.
## Cluster Support
Support for Redis Cluster can be enabled by enabling the `cluster` feature in your Cargo.toml:
`redis = { version = "0.25.3", features = [ "cluster"] }`
Then you can simply use the `ClusterClient`, which accepts a list of available nodes. Note
that only one node in the cluster needs to be specified when instantiating the client, though
you can specify multiple.
```rust
use redis::cluster::ClusterClient;
use redis::Commands;
fn fetch_an_integer() -> String {
let nodes = vec!["redis://127.0.0.1/"];
let client = ClusterClient::new(nodes).unwrap();
let mut connection = client.get_connection().unwrap();
let _: () = connection.set("test", "test_data").unwrap();
let rv: String = connection.get("test").unwrap();
return rv;
}
```
Async Redis Cluster support can be enabled by enabling the `cluster-async` feature, along
with your preferred async runtime, e.g.:
`redis = { version = "0.25.3", features = [ "cluster-async", "tokio-std-comp" ] }`
```rust
use redis::cluster::ClusterClient;
use redis::AsyncCommands;
async fn fetch_an_integer() -> String {
let nodes = vec!["redis://127.0.0.1/"];
let client = ClusterClient::new(nodes).unwrap();
let mut connection = client.get_async_connection().await.unwrap();
let _: () = connection.set("test", "test_data").await.unwrap();
let rv: String = connection.get("test").await.unwrap();
return rv;
}
```
## JSON Support
Support for the RedisJSON Module can be enabled by specifying "json" as a feature in your Cargo.toml.
`redis = { version = "0.25.3", features = ["json"] }`
Then you can simply import the `JsonCommands` trait which will add the `json` commands to all Redis Connections (not to be confused with just `Commands` which only adds the default commands)
```rust
use redis::Client;
use redis::JsonCommands;
use redis::RedisResult;
use redis::ToRedisArgs;
// Result returns Ok(true) if the value was set
// Result returns Err(e) if there was an error with the server itself OR serde_json was unable to serialize the boolean
fn set_json_bool<P: ToRedisArgs>(key: P, path: P, b: bool) -> RedisResult<bool> {
let client = Client::open("redis://127.0.0.1").unwrap();
let connection = client.get_connection().unwrap();
// runs `JSON.SET {key} {path} {b}`
connection.json_set(key, path, b)?
}
```
To parse the results, you'll need to use `serde_json` (or some other json lib) to deserialize
the results from the bytes. It will always be a `Vec`, if no results were found at the path it'll
be an empty `Vec`. If you want to handle deserialization and `Vec` unwrapping automatically,
you can use the `Json` wrapper from the
[redis-macros](https://github.com/daniel7grant/redis-macros/#json-wrapper-with-redisjson) crate.
## Development
To test `redis` you're going to need to be able to test with the Redis Modules, to do this
you must set the following environment variable before running the test script
- `REDIS_RS_REDIS_JSON_PATH` = The absolute path to the RedisJSON module (Either `librejson.so` for Linux or `librejson.dylib` for MacOS).
- Please refer to this [link](https://github.com/RedisJSON/RedisJSON) to access the RedisJSON module:
<!-- As support for modules are added later, it would be wise to update this list -->
If you want to develop on the library there are a few commands provided
by the makefile:
To build:
$ make
To test:
$ make test
To run benchmarks:
$ make bench
To build the docs (require nightly compiler, see [rust-lang/rust#43781](https://github.com/rust-lang/rust/issues/43781)):
$ make docs
We encourage you to run `clippy` prior to seeking a merge for your work. The lints can be quite strict. Running this on your own workstation can save you time, since Travis CI will fail any build that doesn't satisfy `clippy`:
$ cargo clippy --all-features --all --tests --examples -- -D clippy::all -D warnings
To run fuzz tests with afl, first install cargo-afl (`cargo install -f afl`),
then run:
$ make fuzz
If the fuzzer finds a crash, in order to r
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Redis-rs 是 Rust 的高级 redis 库 它通过一个非常灵活但低级的 API 提供对所有 Redis 功能的便捷访 (102个子文件)
array 21B
array-null 5B
bulkstring 12B
bulkstring-null 5B
error 22B
.gitignore 31B
.gitignore 12B
integer 7B
invalid-string 9B
LICENSE 2KB
LICENSE 1KB
LICENSE 10B
LICENSE 10B
Cargo.lock 62KB
Cargo.lock 7KB
Makefile 5KB
CHANGELOG.md 41KB
README.md 8KB
CHANGELOG.md 891B
README.md 57B
README.md 9B
types.rs 86KB
mod.rs 76KB
test_cluster_async.rs 68KB
connection.rs 68KB
mod.rs 54KB
test_basic.rs 52KB
cluster_routing.rs 47KB
test_async.rs 38KB
cluster.rs 37KB
test_cluster.rs 32KB
client.rs 29KB
parser.rs 28KB
sentinel.rs 28KB
mod.rs 26KB
test_types.rs 24KB
streams.rs 21KB
test_streams.rs 20KB
cmd.rs 20KB
multiplexed_connection.rs 20KB
cluster_client.rs 18KB
cluster.rs 17KB
connection.rs 16KB
lib.rs 16KB
test_module_json.rs 16KB
test_sentinel.rs 16KB
json.rs 14KB
sentinel.rs 13KB
lib.rs 12KB
acl.rs 12KB
macros.rs 12KB
pipeline.rs 11KB
connection_manager.rs 11KB
geo.rs 10KB
mock_cluster.rs 10KB
test_async_async_std.rs 10KB
streams.rs 8KB
async_std.rs 8KB
script.rs 7KB
bench_basic.rs 7KB
push_manager.rs 7KB
parser.rs 6KB
tokio.rs 6KB
test_geospatial.rs 6KB
mod.rs 6KB
cluster_pipeline.rs 5KB
basic.rs 5KB
tls.rs 4KB
test_acl.rs 4KB
bench_cluster.rs 3KB
bench_cluster_async.rs 3KB
async-connection-loss.rs 3KB
runtime.rs 2KB
test_script.rs 2KB
test_bignum.rs 2KB
geospatial.rs 2KB
async-multiplexed.rs 1KB
r2d2.rs 1021B
async-pub-sub.rs 620B
async-scan.rs 590B
async-await.rs 557B
reproduce.rs 383B
util.rs 350B
main.rs 136B
macros.rs 120B
lib.rs 1B
update-versions.sh 682B
upload-docs.sh 512B
release.sh 211B
string 5B
Cargo.toml 6KB
Cargo.toml 720B
Cargo.toml 383B
Cargo.toml 273B
.clog.toml 111B
release.toml 76B
Cargo.toml 71B
rustfmt.toml 42B
release.toml 36B
rust.yml 5KB
共 102 条
- 1
- 2
资源评论
AI普惠行者
- 粉丝: 1679
- 资源: 125
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功