31 releases (16 breaking)

0.18.0 Sep 20, 2024
0.16.0 Aug 5, 2024
0.15.1 May 4, 2024
0.15.0 Mar 31, 2024
0.4.1 Dec 31, 2019

#619 in Database interfaces

Download history 29875/week @ 2024-08-22 23700/week @ 2024-08-29 26490/week @ 2024-09-05 23038/week @ 2024-09-12 28272/week @ 2024-09-19 37656/week @ 2024-09-26 22794/week @ 2024-10-03 27937/week @ 2024-10-10 31756/week @ 2024-10-17 26104/week @ 2024-10-24 25867/week @ 2024-10-31 23670/week @ 2024-11-07 63504/week @ 2024-11-14 26980/week @ 2024-11-21 31163/week @ 2024-11-28 27799/week @ 2024-12-05

157,071 downloads per month
Used in 43 crates (31 directly)

MIT/Apache

120KB
2K SLoC

Deadpool for Redis Latest Version Unsafe forbidden Rust 1.75+

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
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
serde Enable support for serde crate deadpool/serde, serde/derive no
cluster Enable support for Redis Cluster redis/cluster-async no

Example

use std::env;

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

#[tokio::main]
async fn main() {
    let mut cfg = Config::from_url(env::var("REDIS__URL").unwrap());
    let pool = cfg.create_pool(Some(Runtime::Tokio1)).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 dotenvy crate

use deadpool_redis::{redis::{cmd, FromRedisValue}, Runtime};
use dotenvy::dotenv;

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

impl Config {
      pub fn from_env() -> Result<Self, config::ConfigError> {
         config::Config::builder()
            .add_source(config::Environment::default().separator("__"))
            .build()?
            .try_deserialize()
    }
}

#[tokio::main]
async fn main() {
    dotenv().ok();
    let cfg = Config::from_env().unwrap();
    let pool = cfg.redis.create_pool(Some(Runtime::Tokio1)).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 (Cluster)

use std::env;
use deadpool_redis::{redis::{cmd, FromRedisValue}};
use deadpool_redis::cluster::{Config, Runtime};

#[tokio::main]
async fn main() {
    let redis_urls = env::var("REDIS_CLUSTER__URLS")
        .unwrap()
        .split(',')
        .map(String::from)
        .collect::<Vec<_>>();
    let mut cfg = Config::from_urls(redis_urls);
    let pool = cfg.create_pool(Some(Runtime::Tokio1)).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 dotenvy crate

use deadpool_redis::redis::{cmd, FromRedisValue};
use deadpool_redis::cluster::{Runtime};
use dotenvy::dotenv;

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

impl Config {
      pub fn from_env() -> Result<Self, config::ConfigError> {
         config::Config::builder()
            .add_source(
                config::Environment::default()
                .separator("__")
                .try_parsing(true)
                .list_separator(","),
            )
            .build()?
            .try_deserialize()
    }
}

#[tokio::main]
async fn main() {
    dotenv().ok();
    let cfg = Config::from_env().unwrap();
    let pool = cfg.redis_cluster.create_pool(Some(Runtime::Tokio1)).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.9", features = ["serde"] }
    redis = { version = "0.21", default-features = false, features = ["tls"] }
    

License

Licensed under either of

at your option.

Dependencies

~8–19MB
~271K SLoC