mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-08-08 05:11:12 +00:00
debug on eagerLoading (1x)
This commit is contained in:
@@ -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 {
|
||||
|
@@ -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(),
|
||||
)
|
Reference in New Issue
Block a user