Skip to content

Commit

Permalink
Fix getMemberOrNull and getGuildMembers caching only user data (#964)
Browse files Browse the repository at this point in the history
As reported in the Kord Discord server [1], these methods of
StoreEntitySupplier are storing the results only to the UserData cache,
not the MemberData cache, so subsequent calls would not find them from
the cache.

When using cacheWithCachingRestFallback, this results in REST calls
being made every single time to get members, unless they got cached from
a gateway event.

[1] https://discord.com/channels/556525343595298817/1271484633610981493
  • Loading branch information
Galarzaa90 authored Aug 11, 2024
1 parent 5bcb0e3 commit 2be545a
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions core/src/commonMain/kotlin/supplier/StoreEntitySupplier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ public class StoreEntitySupplier(

}

override suspend fun getMemberOrNull(guildId: Snowflake, userId: Snowflake): Member? {
return storeAndReturn(supplier.getMemberOrNull(guildId, userId)) { it.data }
}
override suspend fun getMemberOrNull(guildId: Snowflake, userId: Snowflake): Member? =
supplier.getMemberOrNull(guildId, userId)?.also { member ->
cache.put(member.data)
cache.put(member.memberData)
}

override suspend fun getMessageOrNull(channelId: Snowflake, messageId: Snowflake): Message? {
return storeAndReturn(supplier.getMessageOrNull(channelId, messageId)) { it.data }
Expand Down Expand Up @@ -104,9 +106,11 @@ public class StoreEntitySupplier(
return storeOnEach(supplier.getGuildBans(guildId, limit)) { it.data }
}

override fun getGuildMembers(guildId: Snowflake, limit: Int?): Flow<Member> {
return storeOnEach(supplier.getGuildMembers(guildId, limit)) { it.data }
}
override fun getGuildMembers(guildId: Snowflake, limit: Int?): Flow<Member> =
supplier.getGuildMembers(guildId, limit).onEach { member ->
cache.put(member.data)
cache.put(member.memberData)
}

override fun getGuildVoiceRegions(guildId: Snowflake): Flow<Region> {
return storeOnEach(supplier.getGuildVoiceRegions(guildId)) { it.data }
Expand Down

0 comments on commit 2be545a

Please sign in to comment.