Crate deadpool_redis

source ·
Expand description

§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

FeatureDescriptionExtra dependenciesDefault
rt_tokio_1Enable support for tokio cratedeadpool/rt_tokio_1, redis/tokio-compyes
rt_async-std_1Enable support for async-std cratedeadpool/rt_async-std_1, redis/async-std-compno
serdeEnable support for serde cratedeadpool/serde, serde/deriveno
clusterEnable support for Redis Clusterredis/cluster-asyncno

§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.

Re-exports§

Modules§

  • This module extends the library to support Redis Cluster.
  • This module extends the library to support Redis Cluster.

Structs§

Enums§

  • This error is returned if the configuration contains an error
  • This is a 1:1 copy of the redis::ConnectionAddr enumeration (excluding tls_params since it is entirely opaque to consumers).
  • This is a 1:1 copy of the redis::ProtocolVersion struct. Enum representing the communication protocol with the server. This enum represents the types of data that the server can send to the client, and the capabilities that the client can use.
  • Enumeration for picking a runtime implementation.

Type Aliases§