Feature/web interface manager cache requests (#1773)

* Cache webui server mapping file

* Cache webui preview version
This commit is contained in:
schroda
2025-11-09 01:38:25 +01:00
committed by GitHub
parent dd895157c6
commit 3b21442e25

View File

@@ -14,6 +14,7 @@ import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.awaitSuccess import eu.kanade.tachiyomi.network.awaitSuccess
import io.github.oshai.kotlinlogging.KLogger import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLogging import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.reactivecircus.cache4k.Cache
import io.javalin.config.JavalinConfig import io.javalin.config.JavalinConfig
import io.javalin.http.staticfiles.Location import io.javalin.http.staticfiles.Location
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@@ -65,6 +66,7 @@ import java.security.MessageDigest
import java.util.Date import java.util.Date
import kotlin.time.Duration import kotlin.time.Duration
import kotlin.time.Duration.Companion.hours import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
private val applicationDirs: ApplicationDirs by injectLazy() private val applicationDirs: ApplicationDirs by injectLazy()
@@ -91,6 +93,10 @@ object WebInterfaceManager {
private val json: Json by injectLazy() private val json: Json by injectLazy()
private val network: NetworkHelper by injectLazy() private val network: NetworkHelper by injectLazy()
private val CACHE_DURATION = 5.minutes
private val versionMappingCache = Cache.Builder<String, JsonArray>().expireAfterWrite(CACHE_DURATION).build()
private val previewVersionCache = Cache.Builder<String, String>().expireAfterWrite(CACHE_DURATION).build()
private val notifyFlow = MutableSharedFlow<WebUIUpdateStatus?>() private val notifyFlow = MutableSharedFlow<WebUIUpdateStatus?>()
private val statusFlow = MutableSharedFlow<WebUIUpdateStatus>() private val statusFlow = MutableSharedFlow<WebUIUpdateStatus>()
@@ -586,7 +592,10 @@ object WebInterfaceManager {
} }
private suspend fun fetchPreviewVersion(flavor: WebUIFlavor): String = private suspend fun fetchPreviewVersion(flavor: WebUIFlavor): String =
executeWithRetry(KotlinLogging.logger("${logger.name} fetchPreviewVersion(${flavor.uiName})"), { previewVersionCache.get(flavor.uiName) {
executeWithRetry(
KotlinLogging.logger("${logger.name} fetchPreviewVersion(${flavor.uiName})"),
{
val releaseInfoJson = val releaseInfoJson =
network.client network.client
.newCall(GET(flavor.latestReleaseInfoUrl)) .newCall(GET(flavor.latestReleaseInfoUrl))
@@ -595,9 +604,12 @@ object WebInterfaceManager {
.string() .string()
Json.decodeFromString<JsonObject>(releaseInfoJson)["tag_name"]?.jsonPrimitive?.content Json.decodeFromString<JsonObject>(releaseInfoJson)["tag_name"]?.jsonPrimitive?.content
?: throw Exception("Failed to get the preview version tag") ?: throw Exception("Failed to get the preview version tag")
}) },
)
}
private suspend fun fetchServerMappingFile(flavor: WebUIFlavor): JsonArray = private suspend fun fetchServerMappingFile(flavor: WebUIFlavor): JsonArray =
versionMappingCache.get(flavor.uiName) {
executeWithRetry( executeWithRetry(
KotlinLogging.logger("$logger fetchServerMappingFile(${flavor.uiName})"), KotlinLogging.logger("$logger fetchServerMappingFile(${flavor.uiName})"),
{ {
@@ -611,6 +623,7 @@ object WebInterfaceManager {
).jsonArray ).jsonArray
}, },
) )
}
private suspend fun getLatestCompatibleVersion(flavor: WebUIFlavor): String { private suspend fun getLatestCompatibleVersion(flavor: WebUIFlavor): String {
if (serverConfig.webUIChannel.value == WebUIChannel.BUNDLED) { if (serverConfig.webUIChannel.value == WebUIChannel.BUNDLED) {