From: Lanius Trolling Date: Fri, 12 Apr 2024 00:38:47 +0000 (-0400) Subject: Try to fix file cache X-Git-Url: https://gitweb.starshipfights.net/?a=commitdiff_plain;h=6d0e2cd392017a8e18ec5509364a7a85abaff2d4;p=factbooks Try to fix file cache --- diff --git a/src/jvmMain/kotlin/info/mechyrdia/lore/asset_caching.kt b/src/jvmMain/kotlin/info/mechyrdia/lore/asset_caching.kt index 6052dc1..4db2e64 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/lore/asset_caching.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/lore/asset_caching.kt @@ -7,34 +7,29 @@ import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import java.time.Instant import java.util.concurrent.ConcurrentHashMap -import java.util.concurrent.atomic.AtomicLong -import java.util.concurrent.atomic.AtomicReference -import kotlin.math.max val StoragePathAttributeKey = AttributeKey("Mechyrdia.StoragePath") abstract class FileDependentCache { private inner class Entry(updated: Instant?, data: T?) { - private val updatedAtomic = AtomicLong(updated?.toEpochMilli() ?: Long.MIN_VALUE) - val updated: Instant - get() = Instant.ofEpochMilli(updatedAtomic.get()) - - private val dataAtomic = AtomicReference(data) - val data: T? - get() = dataAtomic.get() + private var updated: Instant = updated ?: Instant.MIN + var data: T? = data + private set private val updateLock = Mutex() private fun clear() { - updatedAtomic.set(Long.MIN_VALUE) - dataAtomic.set(null) + updated = Instant.MIN + data = null } suspend fun updateIfNeeded(path: StoragePath): Entry { return updateLock.withLock { - FileStorage.instance.statFile(path)?.updated?.toEpochMilli()?.let { fileUpdated -> - if (updatedAtomic.getAndUpdate { max(it, fileUpdated) } < fileUpdated) - dataAtomic.set(processFile(path)) + FileStorage.instance.statFile(path)?.updated?.let { fileUpdated -> + if (updated < fileUpdated) { + updated = fileUpdated + data = processFile(path) + } this } ?: apply { clear() } }