Skip to main content

aa_storage_redis/
backend.rs

1//! Aggregate handle that builds one connection pool and hands out the three
2//! Redis-backed stores over it.
3
4use aa_storage::Result;
5use deadpool_redis::Pool;
6
7use crate::config::RedisStorageConfig;
8use crate::policy::RedisPolicyStore;
9use crate::pool::build_pool;
10use crate::rate_limit::RedisRateLimitCounter;
11use crate::session::RedisSessionStore;
12
13/// A connected Redis storage driver.
14///
15/// Owns a single [`Pool`] shared by every store it hands out. Cheap to
16/// [`Clone`].
17#[derive(Clone)]
18pub struct RedisBackend {
19    pool: Pool,
20}
21
22impl RedisBackend {
23    /// Build the connection pool described by `config`.
24    ///
25    /// Connections are lazy, so this returns immediately without contacting
26    /// the server (see [`build_pool`]).
27    pub fn connect(config: &RedisStorageConfig) -> Result<Self> {
28        Ok(Self {
29            pool: build_pool(config)?,
30        })
31    }
32
33    /// Borrow the shared connection pool.
34    pub fn pool(&self) -> &Pool {
35        &self.pool
36    }
37
38    /// A [`SessionStore`](aa_storage::SessionStore) over this connection.
39    pub fn sessions(&self) -> RedisSessionStore {
40        RedisSessionStore::new(self.pool.clone())
41    }
42
43    /// A [`RateLimitCounter`](aa_storage::RateLimitCounter) over this connection.
44    pub fn rate_limiter(&self) -> RedisRateLimitCounter {
45        RedisRateLimitCounter::new(self.pool.clone())
46    }
47
48    /// A [`PolicyStore`](aa_storage::PolicyStore) over this connection.
49    pub fn policies(&self) -> RedisPolicyStore {
50        RedisPolicyStore::new(self.pool.clone())
51    }
52}