From 7d1298580135954ec9fffb6d009b64fa58d4da41 Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Wed, 14 Aug 2024 21:18:40 +0900 Subject: [PATCH] asdf15 --- .../chzzk_bot/webserver/utils/DiscordGuildCache.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/DiscordGuildCache.kt b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/DiscordGuildCache.kt index c9c1716..5e253d5 100644 --- a/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/DiscordGuildCache.kt +++ b/webserver/src/main/kotlin/space/mori/chzzk_bot/webserver/utils/DiscordGuildCache.kt @@ -5,6 +5,8 @@ import io.ktor.client.call.* import io.ktor.client.request.* import io.ktor.http.* import kotlinx.coroutines.delay +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.serialization.Serializable import space.mori.chzzk_bot.common.utils.logger import space.mori.chzzk_bot.webserver.DiscordGuildListAPI @@ -15,12 +17,17 @@ import java.util.concurrent.ConcurrentHashMap object DiscordGuildCache { private val cache = ConcurrentHashMap() private const val EXP_SECONDS = 600L + private val mutex = Mutex() suspend fun getCachedGuilds(guildId: String): Guild? { val now = Instant.now() - if(cache.isEmpty() || !cache.containsKey(guildId) || cache[guildId]!!.timestamp.plusSeconds(EXP_SECONDS).isAfter(now)) { - fetchAllGuilds() + if(cache.isEmpty() || !cache.containsKey(guildId) || cache[guildId]!!.timestamp.plusSeconds(EXP_SECONDS).isBefore(now)) { + mutex.withLock { + if(cache.isEmpty() || !cache.containsKey(guildId) || cache[guildId]!!.timestamp.plusSeconds(EXP_SECONDS).isBefore(now)) { + fetchAllGuilds() + } + } } return cache[guildId]?.guild }