diff --git a/src/service.rs b/src/service.rs index 07a48520..42638b2d 100644 --- a/src/service.rs +++ b/src/service.rs @@ -3,9 +3,6 @@ use std::{ sync::{Arc, Mutex as StdMutex, OnceLock}, }; -use lru_cache::LruCache; -use tokio::sync::Mutex; - use crate::{observability::FilterReloadHandles, Config, Result}; pub(crate) mod account_data; @@ -171,9 +168,7 @@ impl Services { threads: rooms::threads::Service { db, }, - spaces: rooms::spaces::Service { - roomid_spacechunk_cache: Mutex::new(LruCache::new(200)), - }, + spaces: rooms::spaces::Service::new(200), user: db, }, transaction_ids: db, diff --git a/src/service/rooms/spaces.rs b/src/service/rooms/spaces.rs index 57655564..ad5f4da0 100644 --- a/src/service/rooms/spaces.rs +++ b/src/service/rooms/spaces.rs @@ -45,11 +45,19 @@ pub(crate) struct CachedSpaceChunk { } pub(crate) struct Service { - pub(crate) roomid_spacechunk_cache: + roomid_spacechunk_cache: Mutex>>, } impl Service { + pub(crate) fn new(roomid_spacechunk_cache_size: usize) -> Self { + Self { + roomid_spacechunk_cache: Mutex::new(LruCache::new( + roomid_spacechunk_cache_size, + )), + } + } + #[allow(clippy::too_many_lines)] #[tracing::instrument(skip(self))] pub(crate) async fn get_hierarchy( @@ -518,6 +526,10 @@ impl Service { }) } + pub(crate) async fn invalidate_cache(&self, room_id: &RoomId) { + self.roomid_spacechunk_cache.lock().await.remove(room_id); + } + fn translate_joinrule(join_rule: &JoinRule) -> Result { match join_rule { JoinRule::Invite => Ok(SpaceRoomJoinRule::Invite), diff --git a/src/service/rooms/state.rs b/src/service/rooms/state.rs index f19a9f9a..4702a76b 100644 --- a/src/service/rooms/state.rs +++ b/src/service/rooms/state.rs @@ -103,10 +103,8 @@ impl Service { services() .rooms .spaces - .roomid_spacechunk_cache - .lock() - .await - .remove(&pdu.room_id); + .invalidate_cache(&pdu.room_id) + .await; } _ => continue, } diff --git a/src/service/rooms/timeline.rs b/src/service/rooms/timeline.rs index d70fd0c2..0cd751dd 100644 --- a/src/service/rooms/timeline.rs +++ b/src/service/rooms/timeline.rs @@ -508,14 +508,12 @@ impl Service { }; } TimelineEventType::SpaceChild => { - if let Some(_state_key) = &pdu.state_key { + if pdu.state_key.is_some() { services() .rooms .spaces - .roomid_spacechunk_cache - .lock() - .await - .remove(&pdu.room_id); + .invalidate_cache(&pdu.room_id) + .await; } } TimelineEventType::RoomMember => {