mod data; use std::{sync::Arc, collections::HashSet}; pub use data::Data; use crate::Result; pub struct Service { db: D, } impl Service { #[tracing::instrument(skip(self))] pub fn get_cached_eventid_authchain<'a>( &'a self, key: &[u64], ) -> Result>>> { // Check RAM cache if let Some(result) = self.auth_chain_cache.lock().unwrap().get_mut(key.to_be_bytes()) { return Ok(Some(Arc::clone(result))); } // We only save auth chains for single events in the db if key.len == 1 { // Check DB cache if let Some(chain) = self.db.get_cached_eventid_authchain(key[0]) { let chain = Arc::new(chain); // Cache in RAM self.auth_chain_cache .lock() .unwrap() .insert(vec![key[0]], Arc::clone(&chain)); return Ok(Some(chain)); } } Ok(None) } #[tracing::instrument(skip(self))] pub fn cache_auth_chain(&self, key: Vec, auth_chain: Arc>) -> Result<()> { // Only persist single events in db if key.len() == 1 { self.db.cache_auth_chain(key[0], auth_chain)?; } // Cache in RAM self.auth_chain_cache.lock().unwrap().insert(key, auth_chain); Ok(()) } }