move appservice_in_room_cache to service

This commit is contained in:
Charles Hall 2024-10-08 15:41:17 -07:00
parent 9d62865b28
commit 107f4521e0
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
3 changed files with 39 additions and 40 deletions

View file

@ -27,6 +27,8 @@ pub(crate) use data::Data;
pub(crate) struct Service {
db: &'static dyn Data,
appservice_in_room_cache:
RwLock<HashMap<OwnedRoomId, HashMap<String, bool>>>,
our_real_users_cache:
RwLock<HashMap<OwnedRoomId, Arc<HashSet<OwnedUserId>>>>,
}
@ -38,6 +40,7 @@ impl Service {
Self {
db,
our_real_users_cache: RwLock::new(HashMap::new()),
appservice_in_room_cache: RwLock::new(HashMap::new()),
}
}
@ -309,6 +312,7 @@ impl Service {
.write()
.unwrap()
.insert(room_id.to_owned(), our_real_users.clone());
self.appservice_in_room_cache.write().unwrap().remove(room_id);
Ok(our_real_users)
}
@ -340,7 +344,31 @@ impl Service {
room_id: &RoomId,
appservice: &RegistrationInfo,
) -> Result<bool> {
self.db.appservice_in_room(room_id, appservice)
let lookup = Lookup::AppserviceInRoom;
if let Some(in_room) = self
.appservice_in_room_cache
.read()
.unwrap()
.get(room_id)
.and_then(|map| map.get(&appservice.registration.id))
.copied()
{
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(in_room);
}
let in_room = self.db.appservice_in_room(room_id, appservice)?;
METRICS.record_lookup(lookup, FoundIn::Database);
self.appservice_in_room_cache
.write()
.unwrap()
.entry(room_id.to_owned())
.or_default()
.insert(appservice.registration.id.clone(), in_room);
Ok(in_room)
}
/// Makes a user forget a room.