1use crate::config::RedisConfig;
4use redis::{aio::ConnectionManager, Client, RedisError};
5use thiserror::Error;
6
7#[derive(Debug, Error)]
9pub enum RedisClientError {
10 #[error("Redis error: {0}")]
11 Redis(#[from] RedisError),
12 #[error("Serialization error: {0}")]
13 Serialization(#[from] serde_json::Error),
14 #[error("Other error: {0}")]
15 Other(String),
16}
17
18#[derive(Clone)]
20pub struct RedisClient {
21 pub(crate) conn: ConnectionManager,
23 pub(crate) config: RedisConfig,
25}
26
27impl RedisClient {
28 pub async fn new(config: RedisConfig) -> Result<Self, RedisClientError> {
30 let url = config.build_connection_url();
31 let client = Client::open(url)?;
32 let conn = ConnectionManager::new(client).await?;
33
34 Ok(Self { conn, config })
35 }
36
37 pub fn key_prefix(&self) -> &str {
39 &self.config.key_prefix
40 }
41
42 pub fn prefixed_key(&self, key: &str) -> String {
44 format!("{}{}", self.key_prefix(), key)
45 }
46}