mod local;
mod redis;
use std::hash::Hash;
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use serde::de::DeserializeOwned;
use serde::Serialize;
pub use local::*;
pub use redis::*;
use crate::{AsyncRedisConnectionManager, RedissonResult};
#[async_trait]
pub trait AsyncCache<K, V> {
async fn get(&self, key: &K) -> RedissonResult<Option<V>>;
async fn set(&self, key: K, value: V) -> RedissonResult<()>;
async fn remove(&self, key: &K) -> RedissonResult<bool>;
async fn clear(&self) -> RedissonResult<()>;
async fn refresh(&self, key: &K) -> RedissonResult<bool>;
}
pub struct AsyncCacheFactory {
connection_manager: Arc<AsyncRedisConnectionManager>,
local_cache_manager: Arc<AsyncLocalCacheManager<String, serde_json::Value>>,
}
impl AsyncCacheFactory {
pub async fn new(connection_manager: Arc<AsyncRedisConnectionManager>) -> Self {
let local_cache_manager = Arc::new(AsyncLocalCacheManager::new(
Duration::from_secs(300), 1000, ));
Self {
connection_manager,
local_cache_manager,
}
}
pub async fn create_cache<K, V>(
&self,
cache_name: &str,
ttl: Duration,
max_size: usize,
) -> Arc<dyn AsyncCache<K, V>>
where
K: Eq + Hash + Clone + Serialize + DeserializeOwned + std::fmt::Debug + Send + Sync + 'static,
V: Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
{
let cache = AsyncRedisIntegratedCache::new(
self.connection_manager.clone(),
cache_name,
ttl,
max_size,
);
Arc::new(cache)
}
pub async fn get_local_cache(&self, name: &str) -> Arc<AsyncLocalCache<String, serde_json::Value>> {
self.local_cache_manager.get_or_create_cache(name).await
}
}