soph_redis/support/
redis.rs1use crate::{config, error::Error, Redis, RedisResult};
2use bb8_redis::{bb8::Pool, redis::cmd, RedisConnectionManager};
3use std::time::Duration;
4
5impl Redis {
6 pub async fn new() -> RedisResult<Self> {
7 let config = soph_config::support::config().parse::<config::Redis>()?;
8
9 let manager = RedisConnectionManager::new(config.url.to_string())?;
10
11 let pool = Pool::builder()
12 .max_size(config.max_size)
13 .min_idle(config.min_idle)
14 .test_on_check_out(config.test_on_check_out)
15 .max_lifetime(Duration::from_millis(config.max_lifetime))
16 .idle_timeout(Duration::from_millis(config.idle_timeout))
17 .connection_timeout(Duration::from_millis(config.connection_timeout))
18 .retry_connection(config.retry_connection)
19 .reaper_rate(Duration::from_millis(config.reaper_rate))
20 .build(manager)
21 .await?;
22
23 Ok(Self { pool })
24 }
25
26 pub async fn ping(&self) -> RedisResult<()> {
28 let mut conn = self.get().await?;
29
30 let reply: String = cmd("PING").query_async(&mut *conn).await?;
31
32 if "PONG" != reply {
33 return Err(Error::RedisPing);
34 }
35
36 Ok(())
37 }
38}
39
40impl std::ops::Deref for Redis {
41 type Target = Pool<RedisConnectionManager>;
42
43 fn deref(&self) -> &Self::Target {
44 &self.pool
45 }
46}