alun_plugin/
cache_plugin.rs1use async_trait::async_trait;
17use alun_core::{Plugin, Result};
18use alun_cache::SharedCache;
19use parking_lot::RwLock;
20
21pub struct CachePlugin {
26 cache: RwLock<Option<SharedCache>>,
28 cache_config: alun_config::CacheConfig,
30 redis_config: alun_config::RedisConfig,
32}
33
34impl CachePlugin {
35 pub fn new(cache_config: &alun_config::CacheConfig, redis_config: &alun_config::RedisConfig) -> Self {
37 Self {
38 cache: RwLock::new(None),
39 cache_config: cache_config.clone(),
40 redis_config: redis_config.clone(),
41 }
42 }
43
44 pub fn cache(&self) -> Option<SharedCache> {
46 self.cache.read().clone()
47 }
48}
49
50#[async_trait]
51impl Plugin for CachePlugin {
52 fn name(&self) -> &str { "cache" }
53
54 async fn start(&self) -> Result<()> {
55 let instance = alun_cache::create_cache(&self.cache_config, &self.redis_config).await?;
56 *self.cache.write() = Some(instance);
57 Ok(())
58 }
59
60 async fn stop(&self) -> Result<()> {
61 *self.cache.write() = None;
62 Ok(())
63 }
64}