11 unstable releases (5 breaking)

new 0.7.1 Feb 22, 2021
0.7.0 Dec 26, 2020
0.6.1 Aug 27, 2020
0.6.0 Jul 14, 2020
0.4.1 Dec 31, 2019

#127 in Database interfaces

Download history 332/week @ 2020-11-07 269/week @ 2020-11-14 374/week @ 2020-11-21 471/week @ 2020-11-28 370/week @ 2020-12-05 397/week @ 2020-12-12 258/week @ 2020-12-19 177/week @ 2020-12-26 367/week @ 2021-01-02 502/week @ 2021-01-09 481/week @ 2021-01-16 562/week @ 2021-01-23 412/week @ 2021-01-30 385/week @ 2021-02-06 533/week @ 2021-02-13 451/week @ 2021-02-20

1,746 downloads per month
Used in 2 crates

MIT/Apache

61KB
831 lines

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

Example

use deadpool_redis::{cmd, Config};
use deadpool_redis::redis::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"])
            .execute_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::cmd;
use deadpool_redis::redis::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"])
            .execute_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.7.1", features = ["config"] }
    redis = { version = "0.20", default-features = false, features = ["tls"] }
    

License

Licensed under either of

at your option.

Dependencies

~7.5MB
~164K SLoC