use crate::config::RedisConfig;
use redis::{aio::ConnectionManager, Client, RedisError};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum RedisClientError {
#[error("Redis error: {0}")]
Redis(#[from] RedisError),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Other error: {0}")]
Other(String),
}
#[derive(Clone)]
pub struct RedisClient {
pub(crate) conn: ConnectionManager,
pub(crate) config: RedisConfig,
}
impl RedisClient {
pub async fn new(config: RedisConfig) -> Result<Self, RedisClientError> {
let url = config.build_connection_url();
let client = Client::open(url)?;
let conn = ConnectionManager::new(client).await?;
Ok(Self { conn, config })
}
pub fn key_prefix(&self) -> &str {
&self.config.key_prefix
}
pub fn prefixed_key(&self, key: &str) -> String {
format!("{}{}", self.key_prefix(), key)
}
}