Skip to main content

yang_db/redis/
config.rs

1use std::time::Duration;
2
3/// Redis 配置
4///
5/// 用于配置 Redis 客户端的连接池和超时参数
6#[derive(Debug, Clone)]
7pub struct RedisConfig {
8    /// 最大连接数
9    pub max_connections: usize,
10    /// 连接超时时间(秒)
11    pub connect_timeout: u64,
12    /// 等待连接超时时间(秒)
13    pub wait_timeout: u64,
14    /// 是否启用日志
15    pub enable_logging: bool,
16}
17
18impl Default for RedisConfig {
19    /// 创建默认配置
20    ///
21    /// # 默认值
22    /// - max_connections: 10
23    /// - connect_timeout: 5 秒
24    /// - wait_timeout: 10 秒
25    /// - enable_logging: false
26    fn default() -> Self {
27        Self {
28            max_connections: 10,
29            connect_timeout: 5,
30            wait_timeout: 10,
31            enable_logging: false,
32        }
33    }
34}
35
36impl RedisConfig {
37    /// 创建新的配置
38    ///
39    /// # 参数
40    /// - `max_connections`: 最大连接数
41    /// - `connect_timeout`: 连接超时时间(秒)
42    /// - `wait_timeout`: 等待连接超时时间(秒)
43    /// - `enable_logging`: 是否启用日志
44    ///
45    /// # 示例
46    /// ```
47    /// use yang_db::RedisConfig;
48    ///
49    /// let config = RedisConfig::new(20, 10, 15, true);
50    /// assert_eq!(config.max_connections, 20);
51    /// assert_eq!(config.connect_timeout, 10);
52    /// ```
53    pub fn new(
54        max_connections: usize,
55        connect_timeout: u64,
56        wait_timeout: u64,
57        enable_logging: bool,
58    ) -> Self {
59        Self {
60            max_connections,
61            connect_timeout,
62            wait_timeout,
63            enable_logging,
64        }
65    }
66
67    /// 获取连接超时 Duration
68    #[allow(dead_code)]
69    pub(crate) fn connect_timeout_duration(&self) -> Duration {
70        Duration::from_secs(self.connect_timeout)
71    }
72
73    /// 获取等待超时 Duration
74    #[allow(dead_code)]
75    pub(crate) fn wait_timeout_duration(&self) -> Duration {
76        Duration::from_secs(self.wait_timeout)
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn test_default_config() {
86        let config = RedisConfig::default();
87        assert_eq!(config.max_connections, 10);
88        assert_eq!(config.connect_timeout, 5);
89        assert_eq!(config.wait_timeout, 10);
90        assert!(!config.enable_logging);
91    }
92
93    #[test]
94    fn test_new_config() {
95        let config = RedisConfig::new(20, 10, 15, true);
96        assert_eq!(config.max_connections, 20);
97        assert_eq!(config.connect_timeout, 10);
98        assert_eq!(config.wait_timeout, 15);
99        assert!(config.enable_logging);
100    }
101
102    #[test]
103    fn test_connect_timeout_duration() {
104        let config = RedisConfig::new(10, 5, 10, false);
105        assert_eq!(config.connect_timeout_duration(), Duration::from_secs(5));
106    }
107
108    #[test]
109    fn test_wait_timeout_duration() {
110        let config = RedisConfig::new(10, 5, 10, false);
111        assert_eq!(config.wait_timeout_duration(), Duration::from_secs(10));
112    }
113
114    #[test]
115    fn test_clone() {
116        let config = RedisConfig::new(15, 8, 12, true);
117        let cloned = config.clone();
118        assert_eq!(config.max_connections, cloned.max_connections);
119        assert_eq!(config.connect_timeout, cloned.connect_timeout);
120        assert_eq!(config.wait_timeout, cloned.wait_timeout);
121        assert_eq!(config.enable_logging, cloned.enable_logging);
122    }
123
124    #[test]
125    fn test_debug() {
126        let config = RedisConfig::default();
127        let debug_str = format!("{:?}", config);
128        assert!(debug_str.contains("RedisConfig"));
129        assert!(debug_str.contains("max_connections"));
130    }
131}