floxide_redis/
client.rs

1//! Redis client for Floxide distributed workflow system.
2
3use crate::config::RedisConfig;
4use redis::{aio::ConnectionManager, Client, RedisError};
5use thiserror::Error;
6
7/// Errors that can occur in the Redis client.
8#[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/// A Redis client for Floxide distributed workflow system.
19#[derive(Clone)]
20pub struct RedisClient {
21    /// The Redis connection manager
22    pub(crate) conn: ConnectionManager,
23    /// The Redis configuration
24    pub(crate) config: RedisConfig,
25}
26
27impl RedisClient {
28    /// Create a new Redis client with the given configuration.
29    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    /// Get the key prefix for Redis keys.
38    pub fn key_prefix(&self) -> &str {
39        &self.config.key_prefix
40    }
41
42    /// Create a prefixed key for Redis.
43    pub fn prefixed_key(&self, key: &str) -> String {
44        format!("{}{}", self.key_prefix(), key)
45    }
46}