diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 11e953981..4eda23b07 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -2,6 +2,7 @@ package io.legado.app.model.analyzeRule import android.text.TextUtils import androidx.annotation.Keep +import com.script.CompiledScript import com.script.buildScriptBindings import com.script.rhino.RhinoScriptEngine import io.legado.app.constant.AppPattern.JS_PATTERN @@ -19,6 +20,7 @@ import io.legado.app.model.webBook.WebBook import io.legado.app.utils.GSON import io.legado.app.utils.NetworkUtils import io.legado.app.utils.fromJsonObject +import io.legado.app.utils.getOrPutLimit import io.legado.app.utils.isJson import io.legado.app.utils.printOnDebug import io.legado.app.utils.splitNotBlank @@ -65,6 +67,7 @@ class AnalyzeRule( private val stringRuleCache = hashMapOf>() private val regexCache = hashMapOf() + private val scriptCache = hashMapOf() private var coroutineContext: CoroutineContext = EmptyCoroutineContext @@ -439,17 +442,7 @@ class AnalyzeRule( } private fun compileRegexCache(regex: String): Regex? { - if (regexCache.size > 16) { - return try { - regex.toRegex() - } catch (e: Exception) { - null - } - } - if (regexCache.containsKey(regex)) { - return regexCache[regex] - } - return regexCache.getOrPut(regex) { + return regexCache.getOrPutLimit(regex, 16) { try { regex.toRegex() } catch (e: Exception) { @@ -769,7 +762,14 @@ class AnalyzeRule( source?.getShareScope(coroutineContext)?.let { scope.prototype = it } - return RhinoScriptEngine.eval(jsStr, scope, coroutineContext) + val script = compileScriptCache(jsStr) + return script.eval(scope, coroutineContext) + } + + private fun compileScriptCache(jsStr: String): CompiledScript { + return scriptCache.getOrPutLimit(jsStr, 16) { + RhinoScriptEngine.compile(jsStr) + } } override fun getSource(): BaseSource? { diff --git a/app/src/main/java/io/legado/app/utils/MapExtensions.kt b/app/src/main/java/io/legado/app/utils/MapExtensions.kt index 6a5464bad..d035b2a73 100644 --- a/app/src/main/java/io/legado/app/utils/MapExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/MapExtensions.kt @@ -16,4 +16,17 @@ fun HashMap.get(key: String, ignoreCase: Boolean = false): T? { } } return null -} \ No newline at end of file +} + +inline fun MutableMap.getOrPutLimit(key: K, maxSize: Int, defaultValue: () -> V): V { + var value = get(key) + if (containsKey(key)) { + @Suppress("UNCHECKED_CAST") + return value as V + } + value = defaultValue() + if (size < maxSize) { + put(key, value) + } + return value +}