move shortstatekey_cache to service

This commit is contained in:
Charles Hall 2024-10-08 13:38:12 -07:00
parent 190b788683
commit d3b62e598d
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
4 changed files with 35 additions and 35 deletions

View file

@ -41,6 +41,8 @@ pub(crate) struct Service {
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
statekeyshort_cache:
Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
shortstatekey_cache:
Mutex<LruCache<ShortStateKey, (StateEventType, String)>>,
}
impl Service {
@ -49,6 +51,7 @@ impl Service {
shorteventid_cache_size: usize,
eventidshort_cache_size: usize,
statekeyshort_cache_size: usize,
shortstatekey_cache_size: usize,
) -> Self {
Self {
db,
@ -61,6 +64,9 @@ impl Service {
statekeyshort_cache: Mutex::new(LruCache::new(
statekeyshort_cache_size,
)),
shortstatekey_cache: Mutex::new(LruCache::new(
shortstatekey_cache_size,
)),
}
}
@ -189,7 +195,25 @@ impl Service {
&self,
shortstatekey: ShortStateKey,
) -> Result<(StateEventType, String)> {
self.db.get_statekey_from_short(shortstatekey)
let lookup = Lookup::ShortToStateKey;
if let Some(id) =
self.shortstatekey_cache.lock().unwrap().get_mut(&shortstatekey)
{
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(id.clone());
}
let x = self.db.get_statekey_from_short(shortstatekey)?;
METRICS.record_lookup(lookup, FoundIn::Database);
self.shortstatekey_cache
.lock()
.unwrap()
.insert(shortstatekey, x.clone());
Ok(x)
}
/// Returns `(shortstatehash, already_existed)`