From aa78bcd4398b3e9775a67a542d11c0acaa82035e Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 13 Apr 2022 21:52:37 +0100 Subject: [PATCH] extracting the room members caching to its own class - need an upper limit to be set --- .../app/dapk/st/matrix/room/RoomService.kt | 3 ++- .../st/matrix/room/internal/RoomMembers.kt | 22 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/RoomService.kt b/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/RoomService.kt index b5eb838..03a7dd9 100644 --- a/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/RoomService.kt +++ b/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/RoomService.kt @@ -10,6 +10,7 @@ import app.dapk.st.matrix.common.RoomMember import app.dapk.st.matrix.common.UserId import app.dapk.st.matrix.room.internal.DefaultRoomService import app.dapk.st.matrix.room.internal.RoomMembers +import app.dapk.st.matrix.room.internal.RoomMembersCache private val SERVICE_KEY = RoomService::class @@ -40,7 +41,7 @@ fun MatrixServiceInstaller.installRoomService( roomMessenger: ServiceDepFactory, ) { this.install { (httpClient, _, services, logger) -> - SERVICE_KEY to DefaultRoomService(httpClient, logger, RoomMembers(memberStore), roomMessenger.create(services)) + SERVICE_KEY to DefaultRoomService(httpClient, logger, RoomMembers(memberStore, RoomMembersCache()), roomMessenger.create(services)) } } diff --git a/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/internal/RoomMembers.kt b/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/internal/RoomMembers.kt index ae156d2..3ee369d 100644 --- a/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/internal/RoomMembers.kt +++ b/matrix/services/room/src/main/kotlin/app/dapk/st/matrix/room/internal/RoomMembers.kt @@ -5,19 +5,17 @@ import app.dapk.st.matrix.common.RoomMember import app.dapk.st.matrix.common.UserId import app.dapk.st.matrix.room.MemberStore -class RoomMembers(private val memberStore: MemberStore) { - - private val cache = mutableMapOf>() +class RoomMembers(private val memberStore: MemberStore, private val membersCache: RoomMembersCache) { suspend fun findMember(roomId: RoomId, userId: UserId): RoomMember? { return findMembers(roomId, listOf(userId)).firstOrNull() } suspend fun findMembers(roomId: RoomId, userIds: List): List { - val roomCache = cache[roomId] + val roomCache = membersCache.room(roomId) return if (roomCache.isNullOrEmpty()) { - memberStore.query(roomId, userIds).also { cache(roomId, it) } + memberStore.query(roomId, userIds).also { membersCache.insert(roomId, it) } } else { val (cachedMembers, missingIds) = userIds.fold(mutableListOf() to mutableListOf()) { acc, current -> when (val member = roomCache[current]) { @@ -29,7 +27,7 @@ class RoomMembers(private val memberStore: MemberStore) { when { missingIds.isNotEmpty() -> { - (memberStore.query(roomId, missingIds).also { cache(roomId, it) } + cachedMembers) + (memberStore.query(roomId, missingIds).also { membersCache.insert(roomId, it) } + cachedMembers) } else -> cachedMembers } @@ -37,13 +35,19 @@ class RoomMembers(private val memberStore: MemberStore) { } suspend fun insert(roomId: RoomId, members: List) { - cache(roomId, members) + membersCache.insert(roomId, members) memberStore.insert(roomId, members) } +} - private fun cache(roomId: RoomId, members: List) { +class RoomMembersCache { + + private val cache = mutableMapOf>() + + fun room(roomId: RoomId) = cache[roomId] + + fun insert(roomId: RoomId, members: List) { val map = cache.getOrPut(roomId) { mutableMapOf() } members.forEach { map[it.id] = it } } - } \ No newline at end of file