mirror of
https://github.com/MewoLab/AquaDX.git
synced 2026-02-09 18:57:27 +08:00
[+] Cache ranking
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package icu.samnyan.aqua.net.utils
|
package icu.samnyan.aqua.net.utils
|
||||||
|
|
||||||
|
import ext.millis
|
||||||
import ext.minus
|
import ext.minus
|
||||||
import icu.samnyan.aqua.net.db.AquaNetUser
|
import icu.samnyan.aqua.net.db.AquaNetUser
|
||||||
import icu.samnyan.aqua.net.games.GenericGameSummary
|
import icu.samnyan.aqua.net.games.GenericGameSummary
|
||||||
@@ -110,10 +111,18 @@ fun genericUserSummary(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val rankingCache = mutableMapOf<String, Pair<Long, List<GenericRankingPlayer>>>()
|
||||||
|
|
||||||
fun genericRanking(
|
fun genericRanking(
|
||||||
userDataRepo: GenericUserDataRepo<*, *>,
|
userDataRepo: GenericUserDataRepo<*, *>,
|
||||||
userPlaylogRepo: GenericPlaylogRepo,
|
userPlaylogRepo: GenericPlaylogRepo,
|
||||||
): List<GenericRankingPlayer> {
|
): List<GenericRankingPlayer> {
|
||||||
|
// Read from cache if we just computed it less than 2 minutes ago
|
||||||
|
val cacheKey = userPlaylogRepo::class.java.name
|
||||||
|
rankingCache[cacheKey]?.let { (t, r) ->
|
||||||
|
if (millis() - t < 120_000) return r
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: pagination
|
// TODO: pagination
|
||||||
val users = userDataRepo.findAll().sortedByDescending { it.playerRating }
|
val users = userDataRepo.findAll().sortedByDescending { it.playerRating }
|
||||||
return users.filter { it.card != null }.mapIndexed { i, user ->
|
return users.filter { it.card != null }.mapIndexed { i, user ->
|
||||||
@@ -129,6 +138,6 @@ fun genericRanking(
|
|||||||
lastSeen = user.lastPlayDate.toString(),
|
lastSeen = user.lastPlayDate.toString(),
|
||||||
username = user.card!!.aquaUser?.username ?: ""
|
username = user.card!!.aquaUser?.username ?: ""
|
||||||
)
|
)
|
||||||
}
|
}.also { rankingCache[cacheKey] = millis() to it } // Update the cache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,14 @@
|
|||||||
package icu.samnyan.aqua.sega.maimai2.handler.impl
|
package icu.samnyan.aqua.sega.maimai2.handler.impl
|
||||||
|
|
||||||
import icu.samnyan.aqua.sega.general.dao.CardRepository
|
|
||||||
import icu.samnyan.aqua.sega.maimai2.handler.BaseHandler
|
import icu.samnyan.aqua.sega.maimai2.handler.BaseHandler
|
||||||
import icu.samnyan.aqua.sega.util.jackson.BasicMapper
|
import icu.samnyan.aqua.sega.util.jackson.BasicMapper
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
import java.time.LocalDateTime
|
|
||||||
import kotlin.jvm.optionals.getOrNull
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author samnyan (privateamusement@protonmail.com)
|
* @author samnyan (privateamusement@protonmail.com)
|
||||||
*/
|
*/
|
||||||
@Component("Maimai2UserLoginHandler")
|
@Component("Maimai2UserLoginHandler")
|
||||||
class UserLoginHandler(mapper: BasicMapper, val cardRepo: CardRepository) : BaseHandler {
|
class UserLoginHandler(mapper: BasicMapper) : BaseHandler {
|
||||||
val resp = mapper.write(mapOf(
|
val resp = mapper.write(mapOf(
|
||||||
"returnCode" to 1,
|
"returnCode" to 1,
|
||||||
"lastLoginDate" to "2020-01-01 00:00:00.0",
|
"lastLoginDate" to "2020-01-01 00:00:00.0",
|
||||||
|
|||||||
Reference in New Issue
Block a user