baichun_framework_cache/redis/
config.rs

1use serde::{Deserialize, Serialize};
2
3/// Redis connection mode
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5pub enum RedisMode {
6    /// Single node
7    Standalone,
8    /// Sentinel mode
9    Sentinel,
10    /// Cluster mode
11    Cluster,
12}
13
14/// Redis node configuration
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct RedisNode {
17    /// Host address
18    pub host: String,
19    /// Port number
20    pub port: u16,
21}
22
23/// Redis sentinel configuration
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct RedisSentinel {
26    /// Master name
27    pub master_name: String,
28    /// Sentinel nodes
29    pub nodes: Vec<RedisNode>,
30    /// Password for sentinel
31    pub password: Option<String>,
32}
33
34/// Redis cluster configuration
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct RedisCluster {
37    /// Cluster nodes
38    pub nodes: Vec<RedisNode>,
39}
40
41/// Redis pool configuration
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct PoolConfig {
44    /// Maximum number of connections
45    pub max_connections: usize,
46    /// Minimum number of connections
47    pub min_connections: u32,
48    /// Connection timeout (seconds)
49    pub connection_timeout: u64,
50    /// Idle timeout (seconds)
51    pub idle_timeout: u64,
52}
53
54impl Default for PoolConfig {
55    fn default() -> Self {
56        Self {
57            max_connections: 10,
58            min_connections: 1,
59            connection_timeout: 10,
60            idle_timeout: 60,
61        }
62    }
63}
64
65/// Redis configuration
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct RedisConfig {
68    /// Redis mode
69    pub mode: RedisMode,
70    /// Single node configuration
71    pub node: Option<RedisNode>,
72    /// Sentinel configuration
73    pub sentinel: Option<RedisSentinel>,
74    /// Cluster configuration
75    pub cluster: Option<RedisCluster>,
76    /// Pool configuration
77    pub pool: PoolConfig,
78    /// Password for Redis
79    pub password: Option<String>,
80    /// Database number
81    pub database: Option<u8>,
82}
83
84impl RedisConfig {
85    /// Validate the configuration
86    pub fn validate(&self) -> crate::redis::Result<()> {
87        match self.mode {
88            RedisMode::Standalone => {
89                if self.node.is_none() {
90                    return Err(crate::redis::RedisError::Config(
91                        "Standalone mode requires node configuration".to_string(),
92                    ));
93                }
94            }
95            RedisMode::Sentinel => {
96                if self.sentinel.is_none() {
97                    return Err(crate::redis::RedisError::Config(
98                        "Sentinel mode requires sentinel configuration".to_string(),
99                    ));
100                }
101            }
102            RedisMode::Cluster => {
103                if self.cluster.is_none() {
104                    return Err(crate::redis::RedisError::Config(
105                        "Cluster mode requires cluster configuration".to_string(),
106                    ));
107                }
108            }
109        }
110        Ok(())
111    }
112}