clm_rust_core/layer0/
cache.rs1use dashmap::DashMap;
2use std::sync::OnceLock;
3use crate::layer0::mcard_record::MCardRecord;
4
5static CARDS_CACHE: OnceLock<DashMap<String, MCardRecord>> = OnceLock::new();
6static HANDLES_CACHE: OnceLock<DashMap<String, String>> = OnceLock::new();
7
8fn get_cards_cache() -> &'static DashMap<String, MCardRecord> {
9 CARDS_CACHE.get_or_init(DashMap::new)
10}
11
12fn get_handles_cache() -> &'static DashMap<String, String> {
13 HANDLES_CACHE.get_or_init(DashMap::new)
14}
15
16pub fn setter_mcard_cache(record: &MCardRecord) -> crate::error::Result<()> {
17 crate::layer0::record_port::require_verified(record)?;
18 get_cards_cache().insert(record.hash.clone(), record.clone());
19 Ok(())
20}
21
22pub fn getter_mcard_cache(reference: &str) -> crate::error::Result<Option<MCardRecord>> {
23 let digest = reference.split_once(':').map_or(reference, |(_, hash)| hash);
24 let record = get_cards_cache().get(reference).or_else(|| get_cards_cache().get(digest)).map(|row| row.value().clone());
25 if let Some(record) = &record {
26 let algorithm = crate::layer0::record_port::require_verified(record)?;
27 if reference.split_once(':').is_some_and(|(prefix, _)| crate::layer0::gtime::canonical_algorithm_name(prefix) != algorithm) {
28 return Err(crate::error::ClmError::Evaluation("cache reference conflicts with g_time".into()));
29 }
30 }
31 Ok(record)
32}
33
34pub fn setter_mcard_cache_handle(handle: &str, hash: &str) {
35 get_handles_cache().insert(handle.to_string(), hash.to_string());
36}
37
38pub fn getter_mcard_cache_handle(handle: &str) -> Option<String> {
39 get_handles_cache().get(handle).map(|r| r.value().clone())
40 .filter(|hash| getter_mcard_cache(hash).is_ok_and(|record| record.is_some()))
41}