mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
asdf15
This commit is contained in:
parent
ac2cd3725c
commit
7d12985801
@ -5,6 +5,8 @@ import io.ktor.client.call.*
|
|||||||
import io.ktor.client.request.*
|
import io.ktor.client.request.*
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import space.mori.chzzk_bot.common.utils.logger
|
import space.mori.chzzk_bot.common.utils.logger
|
||||||
import space.mori.chzzk_bot.webserver.DiscordGuildListAPI
|
import space.mori.chzzk_bot.webserver.DiscordGuildListAPI
|
||||||
@ -15,13 +17,18 @@ import java.util.concurrent.ConcurrentHashMap
|
|||||||
object DiscordGuildCache {
|
object DiscordGuildCache {
|
||||||
private val cache = ConcurrentHashMap<String, CachedGuilds>()
|
private val cache = ConcurrentHashMap<String, CachedGuilds>()
|
||||||
private const val EXP_SECONDS = 600L
|
private const val EXP_SECONDS = 600L
|
||||||
|
private val mutex = Mutex()
|
||||||
|
|
||||||
suspend fun getCachedGuilds(guildId: String): Guild? {
|
suspend fun getCachedGuilds(guildId: String): Guild? {
|
||||||
val now = Instant.now()
|
val now = Instant.now()
|
||||||
|
|
||||||
if(cache.isEmpty() || !cache.containsKey(guildId) || cache[guildId]!!.timestamp.plusSeconds(EXP_SECONDS).isAfter(now)) {
|
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()
|
fetchAllGuilds()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return cache[guildId]?.guild
|
return cache[guildId]?.guild
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user