use core::fmt::Debug;
use crate::core::redis::ReidsClientCluster;
use crate::core::redis::ReidsClientSentinel;
use crate::core::redis::ReidsClientStandalone;
use crate::core::errors::Errors;
#[derive(Clone)]
pub struct ReidsClient {
pub standalone_pool: Option<ReidsClientStandalone>,
pub cluster_pool: Option<ReidsClientCluster>,
pub sentinel_pool: Option<ReidsClientSentinel>,
}
impl ReidsClient {
pub fn publish<T>(&self, topic: &str, data: &T) -> Result<(), Errors>
where
T: serde::Serialize,
{
if let Some(l) = &self.standalone_pool {
return l.publish(topic, data);
}
if let Some(l) = &self.cluster_pool {
return l.publish(topic, data);
}
if let Some(l) = &self.sentinel_pool {
return l.publish(topic, data);
}
Err(Errors::GenericError("not support mode".to_string()))
}
pub fn set<T>(&self, key: &str, value: &T, ttl_sec: usize) -> Result<(), Errors>
where
T: serde::Serialize,
{
if let Some(l) = &self.standalone_pool {
return l.set(key, value, ttl_sec);
}
if let Some(l) = &self.cluster_pool {
return l.set(key, value, ttl_sec);
}
if let Some(l) = &self.sentinel_pool {
return l.set(key, value, ttl_sec);
}
Err(Errors::GenericError("not support mode".to_string()))
}
pub fn get_value(&self, key: &str) -> Result<String, Errors> {
if let Some(l) = &self.standalone_pool {
return l.get_value(key);
}
if let Some(l) = &self.cluster_pool {
return l.get_value(key);
}
if let Some(l) = &self.sentinel_pool {
return l.get_value(key);
}
Err(Errors::GenericError("".to_string()))
}
pub fn get<T>(&self, key: &str) -> Result<T, Errors>
where
T: serde::de::DeserializeOwned + Debug,
{
if let Some(l) = &self.standalone_pool {
return l.get(key);
}
if let Some(l) = &self.cluster_pool {
return l.get(key);
}
if let Some(l) = &self.sentinel_pool {
return l.get(key);
}
Err(Errors::GenericError("".to_string()))
}
}