• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

redis-rs/redis-rs: Redis library for rust

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

redis-rs/redis-rs

开源软件地址(OpenSource Url):

https://github.com/redis-rs/redis-rs

开源编程语言(OpenSource Language):

Rust 99.3%

开源软件介绍(OpenSource Introduction):

redis-rs

Rust crates.io Chat

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:

[dependencies]
redis = "0.21.5"

Documentation on the library can be found at docs.rs/redis.

Note: redis-rs requires at least Rust 1.51.

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.

extern crate redis;
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")
}

Async support

To enable asynchronous clients a feature for the underlying feature need to be activated.

# if you use tokio
redis = { version = "0.17.0", features = ["tokio-comp"] }

# if you use async-std
redis = { version = "0.17.0", features = ["async-std-comp"] }

TLS Support

To enable TLS support, you need to use the relevant feature entry in your Cargo.toml.

redis = { version = "0.19.0", features = ["tls"] }

# if you use tokio
redis = { version = "0.19.0", features = ["tokio-native-tls-comp"] }

# if you use async-std
redis = { version = "0.19.0", features = ["async-std-tls-comp"] }

then you should be able to connect to a redis instance using the rediss:// URL scheme:

let client = redis::Client::open("rediss://127.0.0.1/")?;

Cluster Support

Cluster mode can be used by specifying "cluster" as a features entry in your Cargo.toml.

redis = { version = "0.17.0", features = [ "cluster"] }

Then you can simply use the ClusterClient which accepts a list of available nodes.

use redis::cluster::ClusterClient;
use redis::Commands;

fn fetch_an_integer() -> String {
    // connect to redis
    let nodes = vec!["redis://127.0.0.1/"];
    let client = ClusterClient::open(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;
}

Development

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):

$ 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 reproduce it, run:

$ cd afl/<target>/
$ cargo run --bin reproduce -- out/crashes/<crashfile>



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap