debug on eagerLoading (1x)

This commit is contained in:
dalbodeule
2024-08-28 21:01:35 +09:00
parent a7069046f6
commit 534aaecb6e
3 changed files with 59 additions and 6 deletions

View File

@@ -18,6 +18,7 @@ import space.mori.chzzk_bot.common.services.UserService
import space.mori.chzzk_bot.common.utils.getStreamInfo
import space.mori.chzzk_bot.common.utils.getUserInfo
import space.mori.chzzk_bot.webserver.UserSession
import space.mori.chzzk_bot.webserver.utils.ChzzkUsercache
@Serializable
data class GetUserDTO(
@@ -119,7 +120,7 @@ fun Routing.apiRoutes() {
val subordinates = user.subordinates
println(subordinates)
returnUsers.addAll(subordinates.map {
val subStatus = it.token?.let { it1 -> getStreamInfo(it1) }
val subStatus = it.token?.let { token -> ChzzkUsercache.getCachedUser(token) }
return@map if (it.token == null || subStatus?.content == null) {
null
} else {

View File

@@ -0,0 +1,38 @@
package space.mori.chzzk_bot.webserver.utils
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.common.utils.IData
import space.mori.chzzk_bot.common.utils.IStreamInfo
import space.mori.chzzk_bot.common.utils.getStreamInfo
import java.time.Instant
import java.util.concurrent.ConcurrentHashMap
object ChzzkUsercache {
private val cache = ConcurrentHashMap<String, CachedUser>()
private const val EXP_SECONDS = 600L
private val mutex = Mutex()
private val logger = LoggerFactory.getLogger(this::class.java)
suspend fun getCachedUser(id: String): IData<IStreamInfo?>? {
val now = Instant.now()
var user = cache[id]
if(user == null || user.timestamp.plusSeconds(EXP_SECONDS).isBefore(now)) {
mutex.withLock {
if(user == null || user?.timestamp?.plusSeconds(EXP_SECONDS)?.isBefore(now) != false) {
user = CachedUser(getStreamInfo(id))
user?.let { cache[id] = user!! }
}
}
}
return cache[id]?.user
}
}
data class CachedUser(
val user: IData<IStreamInfo?>,
val timestamp: Instant = Instant.now(),
)