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,28 +1,67 @@
use std::{
collections::{BTreeSet, HashSet},
sync::Arc,
sync::{Arc, Mutex},
};
use lru_cache::LruCache;
use ruma::{api::client::error::ErrorKind, EventId, RoomId};
use tracing::{debug, error, warn};
use super::short::ShortEventId;
use crate::{services, utils::debug_slice_truncated, Error, Result};
use crate::{
observability::{FoundIn, Lookup, METRICS},
services,
utils::debug_slice_truncated,
Error, Result,
};
mod data;
pub(crate) use data::Data;
pub(crate) struct Service {
pub(crate) db: &'static dyn Data,
db: &'static dyn Data,
auth_chain_cache:
Mutex<LruCache<Vec<ShortEventId>, Arc<HashSet<ShortEventId>>>>,
}
impl Service {
pub(crate) fn new(
db: &'static dyn Data,
auth_chain_cache_size: usize,
) -> Self {
Self {
db,
auth_chain_cache: Mutex::new(LruCache::new(auth_chain_cache_size)),
}
}
pub(crate) fn get_cached_eventid_authchain(
&self,
key: &[ShortEventId],
) -> Result<Option<Arc<HashSet<ShortEventId>>>> {
self.db.get_cached_eventid_authchain(key)
let lookup = Lookup::AuthChain;
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)));
}
let Some(chain) = self.db.get_cached_eventid_authchain(key)? else {
METRICS.record_lookup(lookup, FoundIn::Nothing);
return Ok(None);
};
METRICS.record_lookup(lookup, FoundIn::Database);
let chain = Arc::new(chain);
self.auth_chain_cache
.lock()
.unwrap()
.insert(vec![key[0]], Arc::clone(&chain));
Ok(Some(chain))
}
#[tracing::instrument(skip(self))]
@ -31,7 +70,9 @@ impl Service {
key: Vec<ShortEventId>,
auth_chain: Arc<HashSet<ShortEventId>>,
) -> Result<()> {
self.db.cache_auth_chain(key, auth_chain)
self.db.cache_auth_chain(&key, &auth_chain)?;
self.auth_chain_cache.lock().unwrap().insert(key, auth_chain);
Ok(())
}
#[tracing::instrument(