move auth_chain_cache to service

This commit is contained in:
Charles Hall 2024-09-30 21:29:16 -07:00
parent 47502d1f36
commit 095ee483ac
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
5 changed files with 65 additions and 52 deletions

View file

@ -1,8 +1,7 @@
use std::{collections::HashSet, mem::size_of, sync::Arc};
use std::{collections::HashSet, mem::size_of};
use crate::{
database::KeyValueDatabase,
observability::{FoundIn, Lookup, METRICS},
service::{self, rooms::short::ShortEventId},
utils, Result,
};
@ -12,19 +11,9 @@ impl service::rooms::auth_chain::Data for KeyValueDatabase {
fn get_cached_eventid_authchain(
&self,
key: &[ShortEventId],
) -> Result<Option<Arc<HashSet<ShortEventId>>>> {
let lookup = Lookup::AuthChain;
// Check RAM cache
if let Some(result) = self.auth_chain_cache.lock().unwrap().get_mut(key)
{
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(Some(Arc::clone(result)));
}
) -> Result<Option<HashSet<ShortEventId>>> {
// We only save auth chains for single events in the db
if key.len() == 1 {
// Check DB cache
let chain = self
.shorteventid_authchain
.get(&key[0].get().to_be_bytes())?
@ -40,28 +29,16 @@ impl service::rooms::auth_chain::Data for KeyValueDatabase {
.collect()
});
if let Some(chain) = chain {
METRICS.record_lookup(lookup, FoundIn::Database);
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));
}
return Ok(chain);
}
METRICS.record_lookup(lookup, FoundIn::Nothing);
Ok(None)
}
fn cache_auth_chain(
&self,
key: Vec<ShortEventId>,
auth_chain: Arc<HashSet<ShortEventId>>,
key: &[ShortEventId],
auth_chain: &HashSet<ShortEventId>,
) -> Result<()> {
// Only persist single events in db
if key.len() == 1 {
@ -74,9 +51,6 @@ impl service::rooms::auth_chain::Data for KeyValueDatabase {
)?;
}
// Cache in RAM
self.auth_chain_cache.lock().unwrap().insert(key, auth_chain);
Ok(())
}
}