forked from Cookies_Github_mirror/AquaDX
[+] Paged post process
This commit is contained in:
@@ -41,6 +41,7 @@ typealias Bool = Boolean
|
||||
typealias JavaSerializable = java.io.Serializable
|
||||
|
||||
typealias JDict = Map<String, Any?>
|
||||
typealias MutJDict = MutableMap<String, Any?>
|
||||
|
||||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.PROPERTY_GETTER)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
@@ -136,6 +137,7 @@ fun Map<String, Any>.toUrl() = entries.joinToString("&") { (k, v) -> "$k=$v" }
|
||||
fun String.firstCharLower() = replaceFirstChar { it.lowercase() }
|
||||
|
||||
fun Any.long() = when (this) {
|
||||
is Long -> this
|
||||
is Boolean -> if (this) 1L else 0
|
||||
is Number -> toLong()
|
||||
is String -> toLong()
|
||||
|
||||
@@ -88,7 +88,7 @@ val chusanInit: ChusanController.() -> Unit = {
|
||||
// Paged user list endpoints that has a kind in their request
|
||||
"GetUserActivity".pagedWithKind("userActivityList") {
|
||||
val kind = parsing { data["kind"]!!.int }
|
||||
mapOf("kind" to kind) to {
|
||||
mapOf("kind" to kind) grabs {
|
||||
db.userActivity.findAllByUser_Card_ExtIdAndKind(uid, kind).sortedBy { -it.sortNumber }
|
||||
}
|
||||
}
|
||||
@@ -97,15 +97,17 @@ val chusanInit: ChusanController.() -> Unit = {
|
||||
val rawIndex = data["nextIndex"]!!.long
|
||||
val kind = parsing { (rawIndex / 10000000000L).int }
|
||||
data["nextIndex"] = rawIndex % 10000000000L
|
||||
mapOf("itemKind" to kind) to {
|
||||
mapOf("itemKind" to kind) grabs {
|
||||
// TODO: All unlock
|
||||
db.userItem.findAllByUser_Card_ExtIdAndItemKind(uid, kind)
|
||||
} postProcess {
|
||||
it["nextIndex"] = rawIndex + (kind * 10000000000L)
|
||||
}
|
||||
}
|
||||
|
||||
"GetUserFavoriteItem".pagedWithKind("userFavoriteItemList") {
|
||||
val kind = parsing { data["kind"]!!.int }
|
||||
mapOf("kind" to kind) to {
|
||||
mapOf("kind" to kind) grabs {
|
||||
// TODO: Actually store this info at UpsertUserAll
|
||||
val fav = when (kind) {
|
||||
1 -> "favorite_music"
|
||||
|
||||
@@ -25,7 +25,9 @@ typealias SpecialHandler = RequestContext.() -> Any?
|
||||
fun BaseHandler.toSpecial() = { ctx: RequestContext -> handle(ctx.data) }
|
||||
|
||||
typealias PagedHandler = RequestContext.() -> List<Any>
|
||||
typealias AddFn = RequestContext.() -> Pair<Map<String, Any>?, PagedHandler>
|
||||
typealias AddFn = RequestContext.() -> PagedProcessor
|
||||
typealias PagePost = (MutJDict) -> Unit
|
||||
data class PagedProcessor(val add: JDict?, val fn: PagedHandler, var post: PagePost? = null)
|
||||
|
||||
// A very :3 way of declaring APIs
|
||||
abstract class MeowApi(val serialize: (String, Any?) -> String) {
|
||||
@@ -37,14 +39,14 @@ abstract class MeowApi(val serialize: (String, Any?) -> String) {
|
||||
val pageCache = mutableMapOf<String, Pair<Long, List<Any>>>()
|
||||
|
||||
fun String.pagedWithKind(key: String, addFn: AddFn) = this api@ {
|
||||
val (maybeAdd, fn) = addFn()
|
||||
val add = maybeAdd ?: emptyMap()
|
||||
val p = addFn()
|
||||
val add = p.add ?: emptyMap()
|
||||
|
||||
if (nextIndex == -1) return@api fn().let { mapOf("userId" to uid, "length" to it.size, key to it) + add }
|
||||
if (nextIndex == -1) return@api p.fn(this).let { mapOf("userId" to uid, "length" to it.size, key to it) + add }
|
||||
|
||||
// Try to get cache
|
||||
val cacheKey = "$key:$uid:$add"
|
||||
val list = pageCache.getOrPut(cacheKey) { fn().let {
|
||||
val list = pageCache.getOrPut(cacheKey) { p.fn(this).let {
|
||||
if (it.size > maxCount) millis() to it
|
||||
else return@api mapOf("userId" to uid, "length" to it.size, "nextIndex" to -1, key to it) + add
|
||||
} }.r
|
||||
@@ -60,9 +62,12 @@ abstract class MeowApi(val serialize: (String, Any?) -> String) {
|
||||
}
|
||||
else pageCache[cacheKey] = millis() to list
|
||||
|
||||
mapOf("userId" to uid, "length" to lst.size, "nextIndex" to iAfter, key to lst) + add
|
||||
(mapOf("userId" to uid, "length" to lst.size, "nextIndex" to iAfter, key to lst) + add).toMutableMap()
|
||||
.also { p.post?.invoke(it) }
|
||||
}
|
||||
fun String.paged(key: String, fn: PagedHandler) = pagedWithKind(key) { null to fn }
|
||||
fun String.paged(key: String, fn: PagedHandler) = pagedWithKind(key) { PagedProcessor(null, fn) }
|
||||
infix fun JDict.grabs(fn: PagedHandler) = PagedProcessor(this, fn, null)
|
||||
infix fun PagedProcessor.postProcess(post: PagePost) = also { it.post = post }
|
||||
|
||||
// Page cache cleanup every minute
|
||||
@Scheduled(fixedRate = (1000 * 60))
|
||||
|
||||
Reference in New Issue
Block a user