Skip to content

Commit

Permalink
optmize: jar加载缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
Greatwallcorner committed Jul 16, 2024
1 parent 8641815 commit 513630b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,30 @@ object JarLoader {

fun loadJar(key: String, spider: String) {
val texts = spider.split(Constant.md5Split)
// val md5 = if(texts.size<=1) "" else texts[1].trim()
val md5 = if(texts.size<=1) "" else texts[1].trim()
val jar = texts[0]

if(Paths.jar(jar).exists()){
// 可以避免重复下载
if(md5.isNotEmpty() && Utils.equals(parseJarUrl(jar), md5)){
load(key, Paths.jar(jar))
}else if (jar.startsWith("file")) {
load(key, Paths.local(jar))
} else if (jar.startsWith("http")) {
load(key, download(jar))
} else {
loadJar(key, Urls.convert(ApiConfig.api.url!!, spider))
loadJar(key, Urls.convert(ApiConfig.api.url!!, jar))
}

}

/**
* 如果在配置文件种使用的相对路径, 下载的时候使用的全路径 如果的判断md5是否一致的时候使用相对路径 就会造成重复下载
*/
private fun parseJarUrl(jar: String): String {
if(jar.startsWith("file") || jar.startsWith("http")) return jar
return Urls.convert(ApiConfig.api.url!!, jar)
}

private fun load(key: String, jar: File) {
loaders[key] = URLClassLoader(arrayOf(jar.toURI().toURL()),this.javaClass.classLoader)
putProxy(key)
Expand Down Expand Up @@ -83,7 +92,7 @@ object JarLoader {
loader!!.loadClass(classPath).getDeclaredConstructor()
.newInstance() as Spider
spider.init(ext)
spiders.put(spKey, spider)
spiders[spKey] = spider
return spider
} catch (e: Exception) {
e.printStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package com.corner.catvodcore.util
import com.corner.catvodcore.config.ApiConfig
import com.corner.database.Db
import org.apache.commons.lang3.StringUtils
import java.io.File
import java.io.FileInputStream
import java.math.BigInteger
import java.security.MessageDigest
import java.time.Instant
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder
import java.util.*

object Utils {
Expand All @@ -26,6 +25,28 @@ object Utils {

}

fun equals(name: String, md5: String): Boolean {
return md5(Paths.jar(name)).equals(md5, ignoreCase = true)
}

fun md5(file: File?): String {
try {
val digest = MessageDigest.getInstance("MD5")
val fis = FileInputStream(file)
val bytes = ByteArray(4096)
var count: Int
while ((fis.read(bytes).also { count = it }) != -1) digest.update(bytes, 0, count)
fis.close()
val sb = java.lang.StringBuilder()
for (b in digest.digest()) sb.append(((b.toInt() and 0xff) + 0x100).toString(16).substring(1))
return sb.toString()
} catch (e: java.lang.Exception) {
return ""
}
}

// private fun md5()

fun base64(s: String): String {
return base64(s.toByteArray())
}
Expand Down
7 changes: 7 additions & 0 deletions composeApp/src/desktopTest/kotlin/commonTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import com.corner.catvodcore.bean.Result
import com.corner.catvodcore.util.Jsons
import com.corner.catvodcore.util.KtorClient
import com.corner.catvodcore.util.Urls
import com.corner.catvodcore.util.Utils
import com.corner.server.KtorD
import io.ktor.client.request.*
import io.ktor.client.statement.*
Expand All @@ -10,6 +11,7 @@ import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.encodeToJsonElement
import java.io.File
import java.net.URI
import java.net.URL
import java.util.*
Expand Down Expand Up @@ -52,6 +54,11 @@ class commonTest {
// println(path)
}

@Test
fun md5Test(){
println(Utils.md5(File("F:\\sync\\compose-mutiplatform-workspace\\My\\CatVodSpider\\jar\\spider.jar")))
}

@Test
fun splitTest() {
val s = "$$$$$$"
Expand Down

0 comments on commit 513630b

Please sign in to comment.