clm_rust_core/layer0/
cache.rs1use dashmap::DashMap;
2use std::sync::OnceLock;
3use crate::db::get_connection;
4
5static CARDS_CACHE: OnceLock<DashMap<String, String>> = OnceLock::new();
6static HANDLES_CACHE: OnceLock<DashMap<String, String>> = OnceLock::new();
7
8pub fn get_cards_cache() -> &'static DashMap<String, String> {
9 CARDS_CACHE.get_or_init(DashMap::new)
10}
11
12pub fn get_handles_cache() -> &'static DashMap<String, String> {
13 HANDLES_CACHE.get_or_init(DashMap::new)
14}
15
16pub fn setter_mcard_cache(hash: &str, content: &str) {
17 get_cards_cache().insert(hash.to_string(), content.to_string());
18}
19
20pub fn getter_mcard_cache(hash: &str) -> Option<String> {
21 get_cards_cache().get(hash).map(|r| r.value().clone())
22}
23
24pub fn setter_mcard_cache_handle(handle: &str, hash: &str) {
25 get_handles_cache().insert(handle.to_string(), hash.to_string());
26}
27
28pub fn getter_mcard_cache_handle(handle: &str) -> Option<String> {
29 get_handles_cache().get(handle).map(|r| r.value().clone())
30}
31
32pub fn mcard_cache_prepopulate(clm_root: &str) -> crate::error::Result<()> {
33 let conn = get_connection(crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY, clm_root)?;
34
35 let mut stmt = conn
36 .prepare("SELECT handle, current_hash FROM handle_registry")?;
37
38 let rows = stmt
39 .query_map([], |row| {
40 Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
41 })?;
42
43 let handles = get_handles_cache();
44 for (handle, current_hash) in rows.flatten() {
45 handles.insert(handle, current_hash);
46 }
47 Ok(())
48}