baichun_framework_cache/redis/
config.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
5pub enum RedisMode {
6 Standalone,
8 Sentinel,
10 Cluster,
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct RedisNode {
17 pub host: String,
19 pub port: u16,
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct RedisSentinel {
26 pub master_name: String,
28 pub nodes: Vec<RedisNode>,
30 pub password: Option<String>,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct RedisCluster {
37 pub nodes: Vec<RedisNode>,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct PoolConfig {
44 pub max_connections: usize,
46 pub min_connections: u32,
48 pub connection_timeout: u64,
50 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#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct RedisConfig {
68 pub mode: RedisMode,
70 pub node: Option<RedisNode>,
72 pub sentinel: Option<RedisSentinel>,
74 pub cluster: Option<RedisCluster>,
76 pub pool: PoolConfig,
78 pub password: Option<String>,
80 pub database: Option<u8>,
82}
83
84impl RedisConfig {
85 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}