From f20c51c558d87b1124b49fd906865e5976c6a3b3 Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Fri, 25 Dec 2020 18:45:33 +0330 Subject: [PATCH] show correct language on sources, used cached results on getHttpSource to imporve performance a lot --- .../main/kotlin/ir/armor/tachidesk/Main.kt | 14 ++---- .../ir/armor/tachidesk/util/MangaList.kt | 6 +++ .../ir/armor/tachidesk/util/SourceList.kt | 49 +++++++++++++++---- webUI/react/src/components/SourceCard.tsx | 3 +- 4 files changed, 49 insertions(+), 23 deletions(-) diff --git a/server/src/main/kotlin/ir/armor/tachidesk/Main.kt b/server/src/main/kotlin/ir/armor/tachidesk/Main.kt index 9f17d04..c937b54 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/Main.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/Main.kt @@ -1,10 +1,8 @@ package ir.armor.tachidesk import io.javalin.Javalin -import ir.armor.tachidesk.util.applicationSetup -import ir.armor.tachidesk.util.installAPK -import ir.armor.tachidesk.util.getExtensionList -import ir.armor.tachidesk.util.getSourceList +import ir.armor.tachidesk.util.* +import java.util.* class Main { companion object { @@ -38,14 +36,8 @@ class Main { app.get("/api/v1/source/:source_id/popular") { ctx -> val sourceId = ctx.pathParam("source_id") - ctx.json(getPopularManga(sourceId)) +// ctx.json(getPopularManga(sourceId)) } - - - } - - private fun getPopularManga(sourceId: String): List { - TODO("Not yet implemented") } } } diff --git a/server/src/main/kotlin/ir/armor/tachidesk/util/MangaList.kt b/server/src/main/kotlin/ir/armor/tachidesk/util/MangaList.kt index a9e9549..b47a01b 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/util/MangaList.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/util/MangaList.kt @@ -1,2 +1,8 @@ package ir.armor.tachidesk.util +import ir.armor.tachidesk.database.dataclass.MangaDataClass +import ir.armor.tachidesk.database.entity.SourceEntity + +//fun getPopularManga(sourceId: String): List { +// SourceEntity.findById(sourceId.toLong()) +//} \ No newline at end of file diff --git a/server/src/main/kotlin/ir/armor/tachidesk/util/SourceList.kt b/server/src/main/kotlin/ir/armor/tachidesk/util/SourceList.kt index 4955a28..d5c34bc 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/util/SourceList.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/util/SourceList.kt @@ -15,29 +15,58 @@ import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.transaction import java.net.URL import java.net.URLClassLoader +import java.util.* + +private val sourceCache = mutableListOf>() +private val extensionCache = mutableListOf>() fun getHttpSource(sourceId: Long): HttpSource { - return transaction { - val sourceRecord = SourceEntity.get(sourceId) + val cachedResult: Pair? = sourceCache.firstOrNull { it.first == sourceId } + if (cachedResult != null) { + println("used cached HttpSource: ${cachedResult.second.name}") + return cachedResult.second + } + + val result: HttpSource = transaction { + val sourceRecord = SourceEntity.findById(sourceId)!! val extensionId = sourceRecord.extension.id.value - val extensionRecord = ExtensionEntity.get(extensionId) + val extensionRecord = ExtensionEntity.findById(extensionId)!! val apkName = extensionRecord.apkName val className = extensionRecord.classFQName val jarName = apkName.substringBefore(".apk") + ".jar" val jarPath = "${Config.extensionsRoot}/$jarName" - println(jarPath) - - val child = URLClassLoader(arrayOf(URL("file:$jarPath")), this::class.java.classLoader) - val classToLoad = Class.forName(className, true, child) - val instance = classToLoad.newInstance() + println(jarName) + val cachedExtensionPair = extensionCache.firstOrNull { it.first == jarPath } + var usedCached = false + val instance = + if (cachedExtensionPair != null) { + usedCached = true + println("Used cached Extension") + cachedExtensionPair.second + } else { + println("No Extension cache") + val child = URLClassLoader(arrayOf(URL("file:$jarPath")), this::class.java.classLoader) + val classToLoad = Class.forName(className, true, child) + classToLoad.newInstance() + } if (sourceRecord.partOfFactorySource) { - return@transaction (instance as SourceFactory).createSources()[sourceRecord.positionInFactorySource!!] as HttpSource + return@transaction if (usedCached) { + (instance as List)[sourceRecord.positionInFactorySource!!] + } else { + val list = (instance as SourceFactory).createSources() + extensionCache.add(Pair(jarPath, list)) + list[sourceRecord.positionInFactorySource!!] as HttpSource + } } else { + if (!usedCached) + extensionCache.add(Pair(jarPath, instance)) return@transaction instance as HttpSource } } + sourceCache.add(Pair(sourceId, result)) + return result } fun getSourceList(): List { @@ -46,7 +75,7 @@ fun getSourceList(): List { SourceDataClass( it[SourcesTable.id].value, it[SourcesTable.name], - it[SourcesTable.lang], + Locale(it[SourcesTable.lang]).getDisplayLanguage(Locale(it[SourcesTable.lang])), ExtensionsTable.select { ExtensionsTable.id eq it[SourcesTable.extension] }.first()[ExtensionsTable.iconUrl], getHttpSource(it[SourcesTable.id].value).supportsLatest ) diff --git a/webUI/react/src/components/SourceCard.tsx b/webUI/react/src/components/SourceCard.tsx index c889685..a90a29b 100644 --- a/webUI/react/src/components/SourceCard.tsx +++ b/webUI/react/src/components/SourceCard.tsx @@ -44,7 +44,6 @@ export default function SourceCard(props: IProps) { } = props; const classes = useStyles(); - const langPress = lang === 'all' ? 'All' : lang.toUpperCase(); return ( @@ -61,7 +60,7 @@ export default function SourceCard(props: IProps) { {name} - {langPress} + {lang}