1use async_trait::async_trait;
17use parking_lot::RwLock;
18use wordval_core::{Plugin, Result};
19
20use crate::SharedCache;
21
22pub struct CachePlugin {
27 cache: RwLock<Option<SharedCache>>,
29 cache_config: wordval_config::CacheConfig,
31 redis_config: wordval_config::RedisConfig,
33}
34
35impl CachePlugin {
36 pub fn new(
38 cache_config: &wordval_config::CacheConfig,
39 redis_config: &wordval_config::RedisConfig,
40 ) -> Self {
41 Self {
42 cache: RwLock::new(None),
43 cache_config: cache_config.clone(),
44 redis_config: redis_config.clone(),
45 }
46 }
47
48 pub fn cache(&self) -> Option<SharedCache> {
50 self.cache.read().clone()
51 }
52}
53
54#[async_trait]
55impl Plugin for CachePlugin {
56 fn name(&self) -> &str {
57 "cache"
58 }
59
60 fn priority(&self) -> i32 {
61 10
62 }
63
64 async fn start(&self) -> Result<()> {
65 let instance = crate::create_cache(&self.cache_config, &self.redis_config).await?;
66 *self.cache.write() = Some(instance);
67 Ok(())
68 }
69
70 async fn stop(&self) -> Result<()> {
71 *self.cache.write() = None;
72 Ok(())
73 }
74}