zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use crate::core::redis_rs::create_client;
use crate::core::redis_rs::redis_client_cluster::ReidsClientCluster;
use crate::core::redis_rs::redis_client_sentinel::ReidsClientSentinel;
use crate::core::redis_rs::redis_client_standalone::ReidsClientStandalone;

use crate::core::redis_rs::Schema;

#[derive(Clone)]
pub struct ReidsClient {
    pub schema: Schema,
    pub client_standalone: Option<ReidsClientStandalone>,
    pub client_cluster: Option<ReidsClientCluster>,
    pub client_sentinel: Option<ReidsClientSentinel>,
}

impl ReidsClient {
    pub async fn build(server: &str) -> Result<Self, anyhow::Error> {
        create_client(&Schema::parse_schema(server)).await
    }

    pub async fn ttl(&self, key: &str) -> Result<i32, anyhow::Error> {
        if let Some(l) = &self.client_standalone {
            return l.ttl(key).await;
        }

        if let Some(l) = &self.client_cluster {
            return l.ttl(key).await;
        }

        if let Some(l) = &self.client_sentinel {
            return l.ttl(key).await;
        }

        todo!()
    }

    pub async fn exists(&self, key: &str) -> Result<bool, anyhow::Error> {
        if let Some(l) = &self.client_standalone {
            return l.exists(key).await;
        }

        if let Some(l) = &self.client_cluster {
            return l.exists(key).await;
        }

        if let Some(l) = &self.client_sentinel {
            return l.exists(key).await;
        }

        todo!()
    }

    pub async fn set<T>(&self, key: &str, value: &T) -> Result<(), anyhow::Error>
    where
        T: serde::Serialize + std::marker::Sync,
    {
        anyhow::ensure!(!key.trim().is_empty(), "key not be blank!: key={}", key);

        if let Some(l) = &self.client_standalone {
            return l.set(key, value).await;
        }

        if let Some(l) = &self.client_cluster {
            return l.set(key, value).await;
        }

        if let Some(l) = &self.client_sentinel {
            return l.set(key, value).await;
        }

        todo!()
    }

    pub async fn set_ex<T>(&self, key: &str, value: &T, seconds: u64) -> Result<(), anyhow::Error>
    where
        T: serde::Serialize + std::marker::Sync,
    {
        anyhow::ensure!(!key.trim().is_empty(), "key not be blank!: key={}", key);

        if let Some(l) = &self.client_standalone {
            return l.set_ex(key, value, seconds).await;
        }

        if let Some(l) = &self.client_cluster {
            return l.set_ex(key, value, seconds).await;
        }

        if let Some(l) = &self.client_sentinel {
            return l.set_ex(key, value, seconds).await;
        }

        todo!()
    }

    pub async fn get<T>(&self, key: &str) -> Result<Option<T>, anyhow::Error>
    where
        T: serde::de::DeserializeOwned,
    {
        if let Some(l) = &self.client_standalone {
            return l.get(key).await;
        }

        if let Some(l) = &self.client_cluster {
            return l.get(key).await;
        }

        if let Some(l) = &self.client_sentinel {
            return l.get(key).await;
        }

        todo!()
    }
}