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 app_name: String,
28 cache: RwLock<Option<SharedCache>>,
30 cache_config: alun_config::CacheConfig,
32 redis_config: alun_config::RedisConfig,
34}
35
36impl CachePlugin {
37 pub fn new(app_name: &str, cache_config: &alun_config::CacheConfig, redis_config: &alun_config::RedisConfig) -> Self {
41 Self {
42 app_name: app_name.to_string(),
43 cache: RwLock::new(None),
44 cache_config: cache_config.clone(),
45 redis_config: redis_config.clone(),
46 }
47 }
48
49 pub fn cache(&self) -> Option<SharedCache> {
51 self.cache.read().clone()
52 }
53}
54
55#[async_trait]
56impl Plugin for CachePlugin {
57 fn name(&self) -> &str { "cache" }
58
59 async fn start(&self) -> Result<()> {
60 let instance = alun_cache::create_cache(&self.app_name, &self.cache_config, &self.redis_config).await?;
61 *self.cache.write() = Some(instance);
62 Ok(())
63 }
64
65 async fn stop(&self) -> Result<()> {
66 *self.cache.write() = None;
67 Ok(())
68 }
69}