moduforge_rules_engine/loader/
cached.rs1use ahash::{HashMap, HashMapExt};
2use std::future::Future;
3use std::sync::Arc;
4use tokio::sync::Mutex;
5
6use crate::loader::{DecisionLoader, LoaderResponse};
7use crate::model::DecisionContent;
8
9pub struct CachedLoader<Loader: DecisionLoader + 'static> {
10 loader: Arc<Loader>,
11 cache: Mutex<HashMap<String, Arc<DecisionContent>>>,
12}
13
14impl<Loader: DecisionLoader + 'static> From<Arc<Loader>>
15 for CachedLoader<Loader>
16{
17 fn from(value: Arc<Loader>) -> Self {
18 Self { loader: value, cache: Mutex::new(HashMap::new()) }
19 }
20}
21
22impl<Loader: DecisionLoader + 'static> DecisionLoader for CachedLoader<Loader> {
23 fn load<'a>(
24 &'a self,
25 key: &'a str,
26 ) -> impl Future<Output = LoaderResponse> + 'a {
27 async move {
28 let mut cache = self.cache.lock().await;
29 if let Some(content) = cache.get(key) {
30 return Ok(content.clone());
31 }
32
33 let decision_content = self.loader.load(key).await?;
34 cache.insert(key.to_string(), decision_content.clone());
35 Ok(decision_content)
36 }
37 }
38}