19 releases (12 breaking)

0.17.0 Jul 29, 2020
0.15.1 Jan 15, 2020
0.13.0 Oct 14, 2019
0.11.0 Jul 19, 2019
0.0.2 Nov 23, 2014

#2 in Database interfaces

Download history 8096/week @ 2020-06-01 7862/week @ 2020-06-08 7294/week @ 2020-06-15 7386/week @ 2020-06-22 8150/week @ 2020-06-29 7970/week @ 2020-07-06 8011/week @ 2020-07-13 8358/week @ 2020-07-20 9080/week @ 2020-07-27 8382/week @ 2020-08-03 8380/week @ 2020-08-10 9230/week @ 2020-08-17 9463/week @ 2020-08-24 10557/week @ 2020-08-31 10485/week @ 2020-09-07 10641/week @ 2020-09-14

33,291 downloads per month
Used in 121 crates (106 directly)

BSD-3-Clause

345KB
6.5K SLoC

redis-rs

Build Status crates.io

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.17.0"

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

Note: redis-rs requires at least Rust 1.39.

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")
}

Cluster Support

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

redis = { version = "0.16.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:

$ 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>

Dependencies

~2.3–5.5MB
~130K SLoC