Crate deadpool_redis[][src]

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

FeatureDescriptionExtra dependenciesDefault
configEnable support for config crateconfig, serde/deriveyes

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.

Re-exports

pub use redis;

Structs

Cmd

Wrapper for redis::Cmd which makes it compatible with the query_async method which takes a ConnectionLike as argument.

Config

Configuration object. By enabling the config feature you can read the configuration using the config crate.

ConnectionWrapper

A wrapper for redis::Connection. The query_async and execute_async functions of redis::Cmd and redis::Pipeline consume the connection. This wrapper makes it possible to replace the internal connection after executing a query.

Manager

The manager for creating and recyling lapin connections

Pipeline

Wrapper for redis::Cmd which makes it compatible with the query_async method which takes a ConnectionLike as argument.

PoolConfig

Re-export deadpool::managed::PoolConfig

Functions

cmd

Shortcut function to creating a command with a single argument.

pipe

Shortcut for creating a new pipeline.

Type Definitions

Connection

A type alias for using deadpool::Object with redis

Pool

A type alias for using deadpool::Pool with redis

PoolError

A type alias for using deadpool::PoolError with redis