diff --git a/src/database.rs b/src/database.rs index 4979d1e7..0b9d49a0 100644 --- a/src/database.rs +++ b/src/database.rs @@ -7,11 +7,8 @@ use std::{ sync::{Arc, Mutex, RwLock}, }; -use lru_cache::LruCache; use ruma::{ - events::{ - push_rules::PushRulesEvent, GlobalAccountDataEventType, StateEventType, - }, + events::{push_rules::PushRulesEvent, GlobalAccountDataEventType}, push::Ruleset, EventId, OwnedRoomId, OwnedUserId, RoomId, UserId, }; @@ -236,8 +233,6 @@ pub(crate) struct KeyValueDatabase { pub(super) senderkey_pusher: Arc, // Uncategorized trees - pub(super) shortstatekey_cache: - Mutex>, pub(super) our_real_users_cache: RwLock>>>, pub(super) appservice_in_room_cache: @@ -461,14 +456,6 @@ impl KeyValueDatabase { global: builder.open_tree("global")?, server_signingkeys: builder.open_tree("server_signingkeys")?, - #[allow( - clippy::as_conversions, - clippy::cast_sign_loss, - clippy::cast_possible_truncation - )] - shortstatekey_cache: Mutex::new(LruCache::new( - (100_000.0 * config.cache_capacity_modifier) as usize, - )), our_real_users_cache: RwLock::new(HashMap::new()), appservice_in_room_cache: RwLock::new(HashMap::new()), lasttimelinecount_cache: Mutex::new(HashMap::new()), diff --git a/src/database/key_value/rooms/short.rs b/src/database/key_value/rooms/short.rs index ec3fcf8d..ee137733 100644 --- a/src/database/key_value/rooms/short.rs +++ b/src/database/key_value/rooms/short.rs @@ -4,7 +4,6 @@ use ruma::{events::StateEventType, EventId, RoomId}; use crate::{ database::KeyValueDatabase, - observability::{FoundIn, Lookup, METRICS}, service::{ self, rooms::short::{ @@ -130,15 +129,6 @@ impl service::rooms::short::Data for KeyValueDatabase { &self, shortstatekey: ShortStateKey, ) -> Result<(StateEventType, String)> { - 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 bytes = self .shortstatekey_statekey .get(&shortstatekey.get().to_be_bytes())? @@ -168,16 +158,7 @@ impl service::rooms::short::Data for KeyValueDatabase { ) })?; - let result = (event_type, state_key); - - METRICS.record_lookup(lookup, FoundIn::Database); - - self.shortstatekey_cache - .lock() - .unwrap() - .insert(shortstatekey, result.clone()); - - Ok(result) + Ok((event_type, state_key)) } /// Returns `(shortstatehash, already_existed)` diff --git a/src/service.rs b/src/service.rs index f99cac9f..9abbfeaf 100644 --- a/src/service.rs +++ b/src/service.rs @@ -128,6 +128,14 @@ impl Services { { (100_000.0 * config.cache_capacity_modifier) as usize }, + #[allow( + clippy::as_conversions, + clippy::cast_sign_loss, + clippy::cast_possible_truncation + )] + { + (100_000.0 * config.cache_capacity_modifier) as usize + }, ), state: rooms::state::Service { db, diff --git a/src/service/rooms/short.rs b/src/service/rooms/short.rs index dc8ab423..917e5a85 100644 --- a/src/service/rooms/short.rs +++ b/src/service/rooms/short.rs @@ -41,6 +41,8 @@ pub(crate) struct Service { eventidshort_cache: Mutex>, statekeyshort_cache: Mutex>, + shortstatekey_cache: + Mutex>, } 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)`