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 package ir.armor.tachidesk
import io.javalin.Javalin import io.javalin.Javalin
import ir.armor.tachidesk.util.applicationSetup import ir.armor.tachidesk.util.*
import ir.armor.tachidesk.util.installAPK import java.util.*
import ir.armor.tachidesk.util.getExtensionList
import ir.armor.tachidesk.util.getSourceList
class Main { class Main {
companion object { companion object {
@ -38,14 +36,8 @@ class Main {
app.get("/api/v1/source/:source_id/popular") { ctx -> app.get("/api/v1/source/:source_id/popular") { ctx ->
val sourceId = ctx.pathParam("source_id") 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 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 org.jetbrains.exposed.sql.transactions.transaction
import java.net.URL import java.net.URL
import java.net.URLClassLoader 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 { fun getHttpSource(sourceId: Long): HttpSource {
return transaction { val cachedResult: Pair<Long, HttpSource>? = sourceCache.firstOrNull { it.first == sourceId }
val sourceRecord = SourceEntity.get(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 extensionId = sourceRecord.extension.id.value
val extensionRecord = ExtensionEntity.get(extensionId) val extensionRecord = ExtensionEntity.findById(extensionId)!!
val apkName = extensionRecord.apkName val apkName = extensionRecord.apkName
val className = extensionRecord.classFQName val className = extensionRecord.classFQName
val jarName = apkName.substringBefore(".apk") + ".jar" val jarName = apkName.substringBefore(".apk") + ".jar"
val jarPath = "${Config.extensionsRoot}/$jarName" val jarPath = "${Config.extensionsRoot}/$jarName"
println(jarPath) 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 child = URLClassLoader(arrayOf<URL>(URL("file:$jarPath")), this::class.java.classLoader)
val classToLoad = Class.forName(className, true, child) val classToLoad = Class.forName(className, true, child)
val instance = classToLoad.newInstance() classToLoad.newInstance()
}
if (sourceRecord.partOfFactorySource) { if (sourceRecord.partOfFactorySource) {
return@transaction (instance as SourceFactory).createSources()[sourceRecord.positionInFactorySource!!] as HttpSource return@transaction if (usedCached) {
(instance as List<HttpSource>)[sourceRecord.positionInFactorySource!!]
} else { } 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 return@transaction instance as HttpSource
} }
} }
sourceCache.add(Pair(sourceId, result))
return result
} }
fun getSourceList(): List<SourceDataClass> { fun getSourceList(): List<SourceDataClass> {
@ -46,7 +75,7 @@ fun getSourceList(): List<SourceDataClass> {
SourceDataClass( SourceDataClass(
it[SourcesTable.id].value, it[SourcesTable.id].value,
it[SourcesTable.name], 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], ExtensionsTable.select { ExtensionsTable.id eq it[SourcesTable.extension] }.first()[ExtensionsTable.iconUrl],
getHttpSource(it[SourcesTable.id].value).supportsLatest getHttpSource(it[SourcesTable.id].value).supportsLatest
) )

View File

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