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
1,746 downloads per month
Used in 2 crates
61KB
831 lines
Deadpool for Redis 
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
asdeadpool-redis
does and enable the needed features in your ownCrate.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
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Dependencies
~7.5MB
~164K SLoC