baichun-framework-cache 0.1.0

Cache module for Baichun-Rust framework
Documentation
use serde::{Deserialize, Serialize};

/// Redis connection mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RedisMode {
    /// Single node
    Standalone,
    /// Sentinel mode
    Sentinel,
    /// Cluster mode
    Cluster,
}

/// Redis node configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedisNode {
    /// Host address
    pub host: String,
    /// Port number
    pub port: u16,
}

/// Redis sentinel configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedisSentinel {
    /// Master name
    pub master_name: String,
    /// Sentinel nodes
    pub nodes: Vec<RedisNode>,
    /// Password for sentinel
    pub password: Option<String>,
}

/// Redis cluster configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedisCluster {
    /// Cluster nodes
    pub nodes: Vec<RedisNode>,
}

/// Redis pool configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PoolConfig {
    /// Maximum number of connections
    pub max_connections: usize,
    /// Minimum number of connections
    pub min_connections: u32,
    /// Connection timeout (seconds)
    pub connection_timeout: u64,
    /// Idle timeout (seconds)
    pub idle_timeout: u64,
}

impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            max_connections: 10,
            min_connections: 1,
            connection_timeout: 10,
            idle_timeout: 60,
        }
    }
}

/// Redis configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedisConfig {
    /// Redis mode
    pub mode: RedisMode,
    /// Single node configuration
    pub node: Option<RedisNode>,
    /// Sentinel configuration
    pub sentinel: Option<RedisSentinel>,
    /// Cluster configuration
    pub cluster: Option<RedisCluster>,
    /// Pool configuration
    pub pool: PoolConfig,
    /// Password for Redis
    pub password: Option<String>,
    /// Database number
    pub database: Option<u8>,
}

impl RedisConfig {
    /// Validate the configuration
    pub fn validate(&self) -> crate::redis::Result<()> {
        match self.mode {
            RedisMode::Standalone => {
                if self.node.is_none() {
                    return Err(crate::redis::RedisError::Config(
                        "Standalone mode requires node configuration".to_string(),
                    ));
                }
            }
            RedisMode::Sentinel => {
                if self.sentinel.is_none() {
                    return Err(crate::redis::RedisError::Config(
                        "Sentinel mode requires sentinel configuration".to_string(),
                    ));
                }
            }
            RedisMode::Cluster => {
                if self.cluster.is_none() {
                    return Err(crate::redis::RedisError::Config(
                        "Cluster mode requires cluster configuration".to_string(),
                    ));
                }
            }
        }
        Ok(())
    }
}