soph-redis 0.28.2

The RUST Framework for Web Rustceans.
Documentation
use crate::{config, error::Error, Redis, RedisResult};
use bb8_redis::{bb8::Pool, redis::cmd, RedisConnectionManager};
use std::time::Duration;

impl Redis {
    pub async fn new() -> RedisResult<Self> {
        let config = soph_config::support::config().parse::<config::Redis>()?;

        let manager = RedisConnectionManager::new(config.url.to_string())?;

        let pool = Pool::builder()
            .max_size(config.max_size)
            .min_idle(config.min_idle)
            .test_on_check_out(config.test_on_check_out)
            .max_lifetime(Duration::from_millis(config.max_lifetime))
            .idle_timeout(Duration::from_millis(config.idle_timeout))
            .connection_timeout(Duration::from_millis(config.connection_timeout))
            .retry_connection(config.retry_connection)
            .reaper_rate(Duration::from_millis(config.reaper_rate))
            .build(manager)
            .await?;

        Ok(Self { pool })
    }

    // https://docs.rs/bb8-redis/0.15.0/bb8_redis/
    pub async fn ping(&self) -> RedisResult<()> {
        let mut conn = self.get().await?;

        let reply: String = cmd("PING").query_async(&mut *conn).await?;

        if "PONG" != reply {
            return Err(Error::RedisPing);
        }

        Ok(())
    }
}

impl std::ops::Deref for Redis {
    type Target = Pool<RedisConnectionManager>;

    fn deref(&self) -> &Self::Target {
        &self.pool
    }
}