14 breaking releases

Uses old Rust 2015

0.14.0 Feb 28, 2021
0.13.0 Feb 17, 2020
0.12.0 Oct 16, 2019
0.10.1 Jul 19, 2019
0.1.0 Jun 29, 2015

#189 in Database interfaces

Download history 2630/week @ 2021-07-01 2862/week @ 2021-07-08 2954/week @ 2021-07-15 2337/week @ 2021-07-22 2857/week @ 2021-07-29 2726/week @ 2021-08-05 2415/week @ 2021-08-12 2167/week @ 2021-08-19 3071/week @ 2021-08-26 2582/week @ 2021-09-02 3882/week @ 2021-09-09 2602/week @ 2021-09-16 2453/week @ 2021-09-23 2844/week @ 2021-09-30 2645/week @ 2021-10-07 2671/week @ 2021-10-14

8,760 downloads per month
Used in 21 crates (18 directly)

MIT license

6KB
65 lines

r2d2-redis

travis-ci.org MIT licensed crates.io Documentation

redis-rs support library for the r2d2 connection pool totally based on Steven Fackler's r2d2-postgres. All props to him.

Documentation is available here.

r2d2-redis was originally developed by @nevdelap, who has since transferred the repo here due to no longer having enough time to maintain it. Thanks for all your hard work, @nevdelap!

Usage

Add this to your Cargo.toml:

[dependencies]
r2d2_redis = "0.14.0"

Then check out the examples below.

Examples

See examples for runnable examples.

Standard usage

This example shows a standard use case with convenience methods provided by redis::Commands. You'll note that it's practically the same as if you were using the redis crate directly. Thanks to the Deref trait, you'll be able to call any Connection method directly on a pooled connection.

Run with cargo run --example counter:

extern crate r2d2_redis;

use std::thread;

use r2d2_redis::{r2d2, RedisConnectionManager};
use r2d2_redis::redis::Commands;

fn main() {
    let manager = RedisConnectionManager::new("redis://localhost").unwrap();
    let pool = r2d2::Pool::builder()
        .build(manager)
        .unwrap();

    let mut handles = vec![];

    for _i in 0..10i32 {
        let pool = pool.clone();
        handles.push(thread::spawn(move || {
            let mut conn = pool.get().unwrap();
            let n: i64 = conn.incr("counter", 1).unwrap();
            println!("Counter increased to {}", n);
        }));
    }

    for h in handles {
        h.join().unwrap();
    }
}

Manual query building

Unfortunately there are cases when the Deref trait cannot be used. This usually happens when you need to pass the redis connection somewhere else, such as when building queries manually and/or if the redis crate doesn't expose a convenience method for a particular command (e.g. PING). In these cases you must use and call the Deref trait directly.

Run with cargo run --example ping:

extern crate r2d2_redis;

use std::ops::DerefMut;
use std::thread;

use r2d2_redis::{r2d2, redis, RedisConnectionManager};

fn main() {
    let manager = RedisConnectionManager::new("redis://localhost").unwrap();
    let pool = r2d2::Pool::builder()
        .build(manager)
        .unwrap();

    let mut handles = vec![];

    for _i in 0..10i32 {
        let pool = pool.clone();
        handles.push(thread::spawn(move || {
            let mut conn = pool.get().unwrap();
            let reply = redis::cmd("PING").query::<String>(conn.deref_mut()).unwrap();
            // Alternatively, without deref():
            // let reply = redis::cmd("PING").query::<String>(&mut *conn).unwrap();
            assert_eq!("PONG", reply);
        }));
    }

    for h in handles {
        h.join().unwrap();
    }
}

Dependencies

~4.5MB
~111K SLoC