move eventidshort_cache to service

This commit is contained in:
Charles Hall 2024-09-30 21:45:04 -07:00
parent 095ee483ac
commit 2b2b4169df
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
5 changed files with 49 additions and 39 deletions

View file

@ -19,43 +19,26 @@ impl service::rooms::short::Data for KeyValueDatabase {
fn get_or_create_shorteventid(
&self,
event_id: &EventId,
) -> Result<ShortEventId> {
let lookup = Lookup::CreateEventIdToShort;
if let Some(short) =
self.eventidshort_cache.lock().unwrap().get_mut(event_id)
{
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(*short);
}
let short = if let Some(shorteventid) =
) -> Result<(ShortEventId, bool)> {
let (short, created) = if let Some(shorteventid) =
self.eventid_shorteventid.get(event_id.as_bytes())?
{
METRICS.record_lookup(lookup, FoundIn::Database);
let shorteventid =
utils::u64_from_bytes(&shorteventid).map_err(|_| {
Error::bad_database("Invalid shorteventid in db.")
})?;
utils::u64_from_bytes(&shorteventid).map_err(|_| {
Error::bad_database("Invalid shorteventid in db.")
})?
(shorteventid, false)
} else {
METRICS.record_lookup(lookup, FoundIn::Nothing);
let shorteventid = services().globals.next_count()?;
self.eventid_shorteventid
.insert(event_id.as_bytes(), &shorteventid.to_be_bytes())?;
self.shorteventid_eventid
.insert(&shorteventid.to_be_bytes(), event_id.as_bytes())?;
shorteventid
(shorteventid, true)
};
let short = ShortEventId::new(short);
self.eventidshort_cache
.lock()
.unwrap()
.insert(event_id.to_owned(), short);
Ok(short)
Ok((ShortEventId::new(short), created))
}
#[tracing::instrument(skip(self), fields(cache_result))]