From 7122c195f2273116b62597f56893f947adc42280 Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Wed, 10 Apr 2024 19:38:26 -0400 Subject: [PATCH] Fix error --- .../info/mechyrdia/lore/parser_preprocess_include.kt | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) 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)) } } -- 2.25.1