use std::collections::HashMap; use std::sync::{Mutex, OnceLock}; #[derive(Clone, Debug)] pub struct CachedItem { pub content_type: String, pub content: String, pub cached: i64, } /// Determine whether we should actually use the cached item or not. fn should_use(_item: &CachedItem) -> bool { // let current_time: i64 = SystemTime::now() // .duration_since(UNIX_EPOCH) // .expect("SystemTime before UNIX EPOCH!") // .as_secs() // .try_into() // .unwrap(); // current_time <= (item.cached + (2*60)) && item.content != "" true } static CACHE: OnceLock>> = OnceLock::new(); pub async fn get(key: &String) -> Option { let data = CACHE .get_or_init(|| Mutex::new(HashMap::new())) .lock() .unwrap(); dbg!(&key); match data.get(key) { Some(c) => { if should_use(&c) { Some(c.clone()) } else { //let _rm = rm(key.to_string()).await; None } } None => None, } } // async fn rm(key: String) { // let mut data = CACHE.get().unwrap().clone(); // data.remove(&key); // let _ = CACHE.set(data); // } pub async fn set(key: String, item: CachedItem) { let mut data = CACHE .get_or_init(|| Mutex::new(HashMap::new())) .lock() .unwrap(); data.insert(key, item); }