tina-core 0.0.2

Tina platform
Documentation
//! 缓存服务

use core::time::Duration;
use indexmap::{IndexMap, IndexSet};
use serde::{de::DeserializeOwned, Serialize};
use std::{hash::Hash, time::SystemTime};

use crate::tina::{data::AppResult, server::application::Application};

/// 缓存服务
#[async_trait]
pub trait ICacheService: Send + Sync + 'static {
    /// 缓存基本的对象
    async fn set_cache_object<T: Serialize + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        value: &T,
    ) -> AppResult<()>;
    /// 缓存基本的对象
    async fn set_cache_object_with_timeout<T: Serialize + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        value: &T,
        timeout: Duration,
    ) -> AppResult<()>;
    /// 设置有效时间
    async fn expire(&self, application: &Application, key: &str, timeout: Duration) -> AppResult<bool>;
    /// 获得缓存的基本对象
    async fn get_cache_object<T: DeserializeOwned + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
    ) -> AppResult<Option<T>>;
    /// 删除单个对象
    async fn delete_object(&self, application: &Application, key: &str) -> AppResult<bool>;
    /// 删除集合对象
    async fn delete_objects(&self, application: &Application, keys: &[&str]) -> AppResult<i64>;
    /// 缓存集合数据
    async fn set_cache_objects<E: Serialize + Send + Sync + 'static, T: AsRef<[E]> + Send + 'static>(
        &self,
        application: &Application,
        key: &str,
        datas: T,
    ) -> AppResult<i64>;
    /// 获取缓存的集合数据
    async fn get_cache_objects_list<T: DeserializeOwned + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
    ) -> AppResult<Vec<T>>;
    /// 获取缓存的集合数据
    async fn get_cache_objects_set<T: DeserializeOwned + Eq + Hash + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
    ) -> AppResult<IndexSet<T>>;
    /// 缓存Map
    async fn set_cache_map<T: Serialize + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        map: &IndexMap<String, T>,
    ) -> AppResult<()>;
    /// 获得缓存的Map
    async fn get_cache_map<T: DeserializeOwned + Send + Sync + 'static, Map: FromIterator<(String, T)>>(
        &self,
        application: &Application,
        key: &str,
    ) -> AppResult<Map>;

    /// 往Hash中存入数据
    async fn set_cache_map_object<T: Serialize + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        hkey: &str,
        value: &T,
    ) -> AppResult<()>;
    /// 获取Hash中的数据
    async fn get_cache_map_object<T: DeserializeOwned + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        hkey: &str,
    ) -> AppResult<Option<T>>;
    /// 获取多个Hash中的数据
    async fn delete_map_object(&self, application: &Application, key: &str, hkey: &str) -> AppResult<bool>;
    /// 获取多个Hash中的数据
    async fn get_multi_cache_map_object<T: DeserializeOwned + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        hkeys: &[&str],
    ) -> AppResult<Vec<T>>;
    /// 获得缓存的基本对象列表
    async fn keys(&self, application: &Application, pattern: &str) -> AppResult<Vec<String>>;
    /// 获取当前时间
    async fn time(&self, application: &Application) -> AppResult<SystemTime>;
}

/// 无缓存服务
pub struct NoCacheService;

#[allow(unused_variables)]
#[async_trait]
impl ICacheService for NoCacheService {
    /// 缓存基本的对象
    async fn set_cache_object<T: Serialize + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        value: &T,
    ) -> AppResult<()> {
        unimplemented!()
    }
    /// 缓存基本的对象
    async fn set_cache_object_with_timeout<T: Serialize + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        value: &T,
        timeout: Duration,
    ) -> AppResult<()> {
        unimplemented!()
    }
    /// 设置有效时间
    async fn expire(&self, application: &Application, key: &str, timeout: Duration) -> AppResult<bool> {
        unimplemented!()
    }
    /// 获得缓存的基本对象
    async fn get_cache_object<T: DeserializeOwned + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
    ) -> AppResult<Option<T>> {
        unimplemented!()
    }
    /// 删除单个对象
    async fn delete_object(&self, application: &Application, key: &str) -> AppResult<bool> {
        unimplemented!()
    }
    /// 删除集合对象
    async fn delete_objects(&self, application: &Application, keys: &[&str]) -> AppResult<i64> {
        unimplemented!()
    }
    /// 缓存集合数据
    async fn set_cache_objects<E: Serialize + Send + Sync + 'static, T: AsRef<[E]> + Send + 'static>(
        &self,
        application: &Application,
        key: &str,
        datas: T,
    ) -> AppResult<i64> {
        unimplemented!()
    }
    /// 获取缓存的集合数据
    async fn get_cache_objects_list<T: DeserializeOwned + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
    ) -> AppResult<Vec<T>> {
        unimplemented!()
    }
    /// 获取缓存的集合数据
    async fn get_cache_objects_set<T: DeserializeOwned + Eq + Hash + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
    ) -> AppResult<IndexSet<T>> {
        unimplemented!()
    }
    /// 缓存Map
    async fn set_cache_map<T: Serialize + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        map: &IndexMap<String, T>,
    ) -> AppResult<()> {
        unimplemented!()
    }
    /// 获得缓存的Map
    async fn get_cache_map<T: DeserializeOwned + Send + Sync + 'static, Map: FromIterator<(String, T)>>(
        &self,
        application: &Application,
        key: &str,
    ) -> AppResult<Map> {
        unimplemented!()
    }

    /// 往Hash中存入数据
    async fn set_cache_map_object<T: Serialize + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        hkey: &str,
        value: &T,
    ) -> AppResult<()> {
        unimplemented!()
    }
    /// 获取Hash中的数据
    async fn get_cache_map_object<T: DeserializeOwned + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        hkey: &str,
    ) -> AppResult<Option<T>> {
        unimplemented!()
    }
    /// 获取多个Hash中的数据
    async fn delete_map_object(&self, application: &Application, key: &str, hkey: &str) -> AppResult<bool> {
        unimplemented!()
    }
    /// 获取多个Hash中的数据
    async fn get_multi_cache_map_object<T: DeserializeOwned + Send + Sync + 'static>(
        &self,
        application: &Application,
        key: &str,
        hkeys: &[&str],
    ) -> AppResult<Vec<T>> {
        unimplemented!()
    }
    /// 获得缓存的基本对象列表
    async fn keys(&self, application: &Application, pattern: &str) -> AppResult<Vec<String>> {
        unimplemented!()
    }
    /// 获取当前时间
    async fn time(&self, application: &Application) -> AppResult<SystemTime> {
        unimplemented!()
    }
}