use async_trait::async_trait;
use alun_core::{Plugin, Result};
use alun_cache::SharedCache;
use parking_lot::RwLock;
pub struct CachePlugin {
cache: RwLock<Option<SharedCache>>,
cache_config: alun_config::CacheConfig,
redis_config: alun_config::RedisConfig,
}
impl CachePlugin {
pub fn new(cache_config: &alun_config::CacheConfig, redis_config: &alun_config::RedisConfig) -> Self {
Self {
cache: RwLock::new(None),
cache_config: cache_config.clone(),
redis_config: redis_config.clone(),
}
}
pub fn cache(&self) -> Option<SharedCache> {
self.cache.read().clone()
}
}
#[async_trait]
impl Plugin for CachePlugin {
fn name(&self) -> &str { "cache" }
async fn start(&self) -> Result<()> {
let instance = alun_cache::create_cache(&self.cache_config, &self.redis_config).await?;
*self.cache.write() = Some(instance);
Ok(())
}
async fn stop(&self) -> Result<()> {
*self.cache.write() = None;
Ok(())
}
}