#async #redis-cluster #pool

deadpool-redis-cluster

Dead simple async pool for redis-cluster

2 releases

0.1.1 Dec 14, 2023
0.1.0 Sep 26, 2023

#1446 in Database interfaces

Download history 226/week @ 2023-11-08 128/week @ 2023-11-15 58/week @ 2023-11-22 140/week @ 2023-11-29 92/week @ 2023-12-06 107/week @ 2023-12-13 18/week @ 2023-12-20 50/week @ 2023-12-27 44/week @ 2024-01-03 29/week @ 2024-01-10 18/week @ 2024-01-24 11/week @ 2024-01-31 9/week @ 2024-02-07 33/week @ 2024-02-14 71/week @ 2024-02-21

128 downloads per month

MIT/Apache

94KB
1.5K SLoC

Deadpool for Redis Cluster Latest Version Unsafe forbidden Rust 1.63+

⚠️ DEPRECATED! ⚠️

The functionality of redis_cluster_async has been merged into the redis crate rendering this crate obsolete.

Please use deadpool-redis instead.

⚠️ DEPRECATED! ⚠️

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

This crate implements a deadpool manager for redis-cluster.

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

Example

use std::env;
use deadpool_redis_cluster::{redis::{cmd, FromRedisValue}, 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 dotenv crate

use deadpool_redis_cluster::{redis::{cmd, FromRedisValue}, Runtime};
use dotenv::dotenv;
# use serde_1 as serde;

#[derive(Debug, serde::Deserialize)]
# #[serde(crate = "serde_1")]
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-cluster does and enable the needed features in your own Crate.toml file:

    [dependencies]
    deadpool-redis-cluster = { version = "0.9", features = ["serde"] }
    redis = { version = "0.21", default-features = false, features = ["tls"] }
    

License

Licensed under either of

at your option.

Dependencies

~7–20MB
~296K SLoC