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

@ -1,7 +1,7 @@
use std::sync::{Arc, Mutex};
use lru_cache::LruCache;
use ruma::{events::StateEventType, EventId, RoomId};
use ruma::{events::StateEventType, EventId, OwnedEventId, RoomId};
use crate::{
observability::{FoundIn, Lookup, METRICS},
@ -38,18 +38,23 @@ pub(crate) use data::Data;
pub(crate) struct Service {
db: &'static dyn Data,
shorteventid_cache: Mutex<LruCache<ShortEventId, Arc<EventId>>>,
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
}
impl Service {
pub(crate) fn new(
db: &'static dyn Data,
shorteventid_cache_size: usize,
eventidshort_cache_size: usize,
) -> Self {
Self {
db,
shorteventid_cache: Mutex::new(LruCache::new(
shorteventid_cache_size,
)),
eventidshort_cache: Mutex::new(LruCache::new(
eventidshort_cache_size,
)),
}
}
@ -57,7 +62,29 @@ impl Service {
&self,
event_id: &EventId,
) -> Result<ShortEventId> {
self.db.get_or_create_shorteventid(event_id)
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, created) = self.db.get_or_create_shorteventid(event_id)?;
if created {
METRICS.record_lookup(lookup, FoundIn::Nothing);
} else {
METRICS.record_lookup(lookup, FoundIn::Database);
}
self.eventidshort_cache
.lock()
.unwrap()
.insert(event_id.to_owned(), short);
Ok(short)
}
pub(crate) fn get_shortstatekey(