7ea2fcc
pub mod config;
pub mod hybrid;
pub mod redis;
pub mod typed;
use crate::config::cache::AppCacheSettings;
use crate::config::redis::AppRedisSettings;
pub use hybrid::Cache;
pub use redis::RedisCache;
pub use typed::TypedCache;
pub struct CacheBuilder {
cache_cfg: AppCacheSettings,
redis_cfg: Option<AppRedisSettings>,
}
impl CacheBuilder {
pub fn from_config(cache_cfg: AppCacheSettings) -> Self {
Self {
cache_cfg,
redis_cfg: None,
}
}
pub fn with_redis(mut self, redis_cfg: AppRedisSettings) -> Self {
self.redis_cfg = Some(redis_cfg);
self
}
pub async fn build(self) -> Result<Option<Cache>, CacheError> {
if !self.cache_cfg.enabled {
return Ok(None);
}
let hybrid = hybrid::new_hybrid(&self.cache_cfg).await?;
let l3 = if let Some(ref rc) = self.redis_cfg {
if !rc.urls.is_empty() {
Some(RedisCache::new(rc).await?)
} else {
None
}
} else {
None
};
Ok(Some(Cache::new(hybrid, l3, self.cache_cfg.ttl_seconds)))
}
}
#[derive(Debug, thiserror::Error)]
pub enum CacheError {
#[error("hybrid cache error: {0}")]
Hybrid(#[from] bincache::Error),
#[error("redis cache error: {0}")]
Redis(#[from] redis::RedisError),
}