show correct language on sources, used cached results on getHttpSource to imporve performance a lot

This commit is contained in:
Aria Moradi 2020-12-25 18:45:33 +03:30
parent ab33a0ef1d
commit f20c51c558
4 changed files with 49 additions and 23 deletions

View File

@ -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<Any> {
TODO("Not yet implemented")
}
}
}

View File

@ -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<MangaDataClass> {
// SourceEntity.findById(sourceId.toLong())
//}

View File

@ -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<Pair<Long, HttpSource>>()
private val extensionCache = mutableListOf<Pair<String, Any>>()
fun getHttpSource(sourceId: Long): HttpSource {
return transaction {
val sourceRecord = SourceEntity.get(sourceId)
val cachedResult: Pair<Long, HttpSource>? = 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>(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>(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<HttpSource>)[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<SourceDataClass> {
@ -46,7 +75,7 @@ fun getSourceList(): List<SourceDataClass> {
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
)

View File

@ -44,7 +44,6 @@ export default function SourceCard(props: IProps) {
} = props;
const classes = useStyles();
const langPress = lang === 'all' ? 'All' : lang.toUpperCase();
return (
<Card>
@ -61,7 +60,7 @@ export default function SourceCard(props: IProps) {
{name}
</Typography>
<Typography variant="caption" display="block" gutterBottom>
{langPress}
{lang}
</Typography>
</div>
</div>