16 unstable releases (7 breaking)

0.9.0 Aug 19, 2021
0.8.1 Jun 1, 2021
0.8.0 May 20, 2021
0.7.1 Feb 22, 2021
0.4.1 Dec 31, 2019

#93 in Database interfaces

Download history 1458/week @ 2021-06-01 1797/week @ 2021-06-08 2022/week @ 2021-06-15 1956/week @ 2021-06-22 1856/week @ 2021-06-29 1452/week @ 2021-07-06 2437/week @ 2021-07-13 1646/week @ 2021-07-20 1996/week @ 2021-07-27 2483/week @ 2021-08-03 1967/week @ 2021-08-10 1571/week @ 2021-08-17 1692/week @ 2021-08-24 1475/week @ 2021-08-31 1357/week @ 2021-09-07 1309/week @ 2021-09-14

7,933 downloads per month
Used in 4 crates

MIT/Apache

76KB
1K SLoC

Deadpool for Redis Latest Version

Deadpool is a dead simple async pool for connections and objects of any type.

This crate implements a deadpool manager for redis.

Features

Feature Description Extra dependencies Default
config Enable support for config crate config, serde/derive yes
rt_tokio_1 Enable support for tokio crate deadpool/rt_tokio_1, redis/tokio-comp yes
rt_async-std_1 Enable support for async-std crate deadpool/rt_async-std_1, redis/async-std-comp no

Example

use deadpool_redis::redis::{cmd, Config, FromRedisValue};

#[tokio::main]
async fn main() {
    let mut cfg = Config::default();
    cfg.url = Some("redis://127.0.0.1/".to_string());
    let pool = cfg.create_pool().unwrap();
    {
        let mut conn = pool.get().await.unwrap();
        cmd("SET")
            .arg(&["deadpool/test_key", "42"])
            .query_async::<_, ()>(&mut conn)
            .await.unwrap();
    }
    {
        let mut conn = pool.get().await.unwrap();
        let value: String = cmd("GET")
            .arg(&["deadpool/test_key"])
            .query_async(&mut conn)
            .await.unwrap();
        assert_eq!(value, "42".to_string());
    }
}

Example with config and dotenv crate

use deadpool_redis::redis::{cmd, FromRedisValue};
use dotenv::dotenv;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Config {
    #[serde(default)]
    redis: deadpool_redis::Config
}

impl Config {
    pub fn from_env() -> Result<Self, ::config_crate::ConfigError> {
        let mut cfg = ::config_crate::Config::new();
        cfg.merge(::config_crate::Environment::new().separator("__"))?;
        cfg.try_into()
    }
}

#[tokio::main]
async fn main() {
    dotenv().ok();
    let cfg = Config::from_env().unwrap();
    let pool = cfg.redis.create_pool().unwrap();
    {
        let mut conn = pool.get().await.unwrap();
        cmd("SET")
            .arg(&["deadpool/test_key", "42"])
            .query_async::<_, ()>(&mut conn)
            .await.unwrap();
    }
    {
        let mut conn = pool.get().await.unwrap();
        let value: String = cmd("GET")
            .arg(&["deadpool/test_key"])
            .query_async(&mut conn)
            .await.unwrap();
        assert_eq!(value, "42".to_string());
    }
}

FAQ

  • How can I enable features of the redis crate?

    Make sure that you depend on the same version of redis as deadpool-redis does and enable the needed features in your own Crate.toml file:

    [dependencies]
    deadpool-redis = { version = "0.8", features = ["config"] }
    redis = { version = "0.20", default-features = false, features = ["tls"] }
    

License

Licensed under either of

at your option.

Dependencies

~6.5–9MB
~194K SLoC