1use crate::cmd::RedisClient;
2use redis::aio::ConnectionManager;
3use serde::Deserialize;
4use std::fs::read_to_string;
5
6#[derive(Debug, Default, Clone, Deserialize)]
7pub struct RedisDB {
8 pub nodes: Option<Vec<String>>,
9 pub password: Option<String>,
10 pub node: Option<String>,
11}
12
13pub fn load_redis_config(filename: &str) -> RedisDB {
14 let content = read_to_string(&filename).expect(&format!("read {} error", filename));
15 let redis_db: RedisDB =
16 toml::from_str(&content).expect(&format!("{} config init err", filename));
17 redis_db
18}
19
20impl RedisDB {
21 pub async fn load_redis_pool(&self) -> redis::RedisResult<RedisClient> {
22 #[cfg(feature = "cluster")]
23 let client = redis::cluster::ClusterClientBuilder::new(self.nodes.unwrap().to_owned())
24 .password(redis_config.password.to_owned().unwrap_or_default())
25 .build()?;
26
27 #[cfg(feature = "single")]
28 let client = self.manager_client().await?;
29
30 Ok(RedisClient { client })
31 }
32
33 #[cfg(feature = "single")]
34 async fn manager_client(&self) -> redis::RedisResult<ConnectionManager> {
35 let client = redis::Client::open(self.node.as_ref().unwrap().as_str())?;
36 Ok(ConnectionManager::new(client).await?)
37 }
38}