add constructor for spaces service

Also adds a public function to invalidate the cache rather than exposing
the entire cache publicly.
This commit is contained in:
Charles Hall 2024-10-08 16:31:57 -07:00
parent f702b6cccd
commit f771d319b2
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
4 changed files with 19 additions and 16 deletions

View file

@ -3,9 +3,6 @@ use std::{
sync::{Arc, Mutex as StdMutex, OnceLock}, sync::{Arc, Mutex as StdMutex, OnceLock},
}; };
use lru_cache::LruCache;
use tokio::sync::Mutex;
use crate::{observability::FilterReloadHandles, Config, Result}; use crate::{observability::FilterReloadHandles, Config, Result};
pub(crate) mod account_data; pub(crate) mod account_data;
@ -171,9 +168,7 @@ impl Services {
threads: rooms::threads::Service { threads: rooms::threads::Service {
db, db,
}, },
spaces: rooms::spaces::Service { spaces: rooms::spaces::Service::new(200),
roomid_spacechunk_cache: Mutex::new(LruCache::new(200)),
},
user: db, user: db,
}, },
transaction_ids: db, transaction_ids: db,

View file

@ -45,11 +45,19 @@ pub(crate) struct CachedSpaceChunk {
} }
pub(crate) struct Service { pub(crate) struct Service {
pub(crate) roomid_spacechunk_cache: roomid_spacechunk_cache:
Mutex<LruCache<OwnedRoomId, Option<CachedSpaceChunk>>>, Mutex<LruCache<OwnedRoomId, Option<CachedSpaceChunk>>>,
} }
impl Service { 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)] #[allow(clippy::too_many_lines)]
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) async fn get_hierarchy( 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<SpaceRoomJoinRule> { fn translate_joinrule(join_rule: &JoinRule) -> Result<SpaceRoomJoinRule> {
match join_rule { match join_rule {
JoinRule::Invite => Ok(SpaceRoomJoinRule::Invite), JoinRule::Invite => Ok(SpaceRoomJoinRule::Invite),

View file

@ -103,10 +103,8 @@ impl Service {
services() services()
.rooms .rooms
.spaces .spaces
.roomid_spacechunk_cache .invalidate_cache(&pdu.room_id)
.lock() .await;
.await
.remove(&pdu.room_id);
} }
_ => continue, _ => continue,
} }

View file

@ -508,14 +508,12 @@ impl Service {
}; };
} }
TimelineEventType::SpaceChild => { TimelineEventType::SpaceChild => {
if let Some(_state_key) = &pdu.state_key { if pdu.state_key.is_some() {
services() services()
.rooms .rooms
.spaces .spaces
.roomid_spacechunk_cache .invalidate_cache(&pdu.room_id)
.lock() .await;
.await
.remove(&pdu.room_id);
} }
} }
TimelineEventType::RoomMember => { TimelineEventType::RoomMember => {