mirror of
https://github.com/ouchadam/small-talk.git
synced 2024-12-23 00:09:59 +01:00
extracting the room members caching to its own class - need an upper limit to be set
This commit is contained in:
parent
dccf8cc206
commit
aa78bcd439
@ -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<RoomMessenger>,
|
||||
) {
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<RoomId, MutableMap<UserId, RoomMember>>()
|
||||
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<UserId>): List<RoomMember> {
|
||||
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<RoomMember>() to mutableListOf<UserId>()) { 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<RoomMember>) {
|
||||
cache(roomId, members)
|
||||
membersCache.insert(roomId, members)
|
||||
memberStore.insert(roomId, members)
|
||||
}
|
||||
}
|
||||
|
||||
private fun cache(roomId: RoomId, members: List<RoomMember>) {
|
||||
class RoomMembersCache {
|
||||
|
||||
private val cache = mutableMapOf<RoomId, MutableMap<UserId, RoomMember>>()
|
||||
|
||||
fun room(roomId: RoomId) = cache[roomId]
|
||||
|
||||
fun insert(roomId: RoomId, members: List<RoomMember>) {
|
||||
val map = cache.getOrPut(roomId) { mutableMapOf() }
|
||||
members.forEach { map[it.id] = it }
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user