From: Lanius Trolling Date: Wed, 10 Apr 2024 23:38:26 +0000 (-0400) Subject: Fix error X-Git-Url: https://gitweb.starshipfights.net/?a=commitdiff_plain;h=7122c195f2273116b62597f56893f947adc42280;p=factbooks Fix error --- diff --git a/src/jvmMain/kotlin/info/mechyrdia/lore/parser_preprocess_include.kt b/src/jvmMain/kotlin/info/mechyrdia/lore/parser_preprocess_include.kt index ee15e45..d7ef1cf 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/lore/parser_preprocess_include.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/lore/parser_preprocess_include.kt @@ -3,20 +3,19 @@ package info.mechyrdia.lore import info.mechyrdia.JsonStorageCodec import info.mechyrdia.data.FileStorage import info.mechyrdia.data.StoragePath -import io.github.reactivecircus.cache4k.Cache import io.ktor.util.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withContext import kotlinx.serialization.json.* import java.security.MessageDigest +import java.util.concurrent.ConcurrentHashMap import java.util.function.Consumer import javax.script.Compilable import javax.script.CompiledScript import javax.script.ScriptEngineManager import javax.script.SimpleBindings import kotlin.coroutines.* -import kotlin.time.Duration.Companion.days object PreProcessorTemplateLoader { suspend fun loadTemplate(name: String): ParserTree { @@ -37,18 +36,15 @@ object PreProcessorTemplateLoader { object PreProcessorScriptLoader { private val scriptEngine = ThreadLocal.withInitial { ScriptEngineManager().getEngineByExtension("groovy") } private val hasher = ThreadLocal.withInitial { MessageDigest.getInstance("SHA-256") } - private val cache = Cache.Builder() - .maximumCacheSize(16) - .expireAfterAccess(72.days) - .build() + private val cache = ConcurrentHashMap() suspend fun loadFunction(name: String): CompiledScript? { val scriptFile = StoragePath.scriptDir / "$name.groovy" val script = FileStorage.instance.readFile(scriptFile) ?: return null val digest = hex(hasher.get().digest(script)) - return cache.get(digest) { - withContext(Dispatchers.IO) { + return withContext(Dispatchers.IO) { + cache.computeIfAbsent(digest) { _ -> (scriptEngine.get() as Compilable).compile(String(script)) } }