admin_config/
redis_config.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub struct RedisConfig {
5    /// 主机地址
6    pub host: String,
7    /// 端口
8    pub port: u16,
9    /// 用户名
10    pub username: Option<String>,
11    /// 密码
12    pub password: Option<String>,
13    /// 数据库索引
14    pub database: u8,
15    /// 最大连接池大小
16    pub max_pool_size: u32,
17    /// 连接超时时间(秒)
18    pub connect_timeout: u64,
19}
20
21impl Default for RedisConfig {
22    fn default() -> Self {
23        Self {
24            host: "localhost".to_string(),
25            port: 6379,
26            username: Some("".to_string()),
27            password: Some("".to_string()),
28            database: 0,
29            max_pool_size: 20,
30            connect_timeout: 5,
31        }
32    }
33}
34
35impl RedisConfig {
36    pub fn connection_string(&self) -> String {
37        match (&self.username, &self.password) {
38            (Some(username), Some(password)) => {
39                format!(
40                    "redis://{}:{}@{}:{}/{}",
41                    username, password, self.host, self.port, self.database
42                )
43            }
44            (None, Some(password)) => {
45                format!("redis://:{}@{}:{}/{}", password, self.host, self.port, self.database)
46            }
47            _ => {
48                format!("redis://{}:{}/{}", self.host, self.port, self.database)
49            }
50        }
51    }
52}