use async_trait::async_trait;
use parking_lot::RwLock;
use wordval_core::{Plugin, Result};
use crate::SharedCache;
pub struct CachePlugin {
cache: RwLock<Option<SharedCache>>,
cache_config: wordval_config::CacheConfig,
redis_config: wordval_config::RedisConfig,
}
impl CachePlugin {
pub fn new(
cache_config: &wordval_config::CacheConfig,
redis_config: &wordval_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"
}
fn priority(&self) -> i32 {
10
}
async fn start(&self) -> Result<()> {
let instance = crate::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(())
}
}