Try to fix file cache
authorLanius Trolling <lanius@laniustrolling.dev>
Fri, 12 Apr 2024 00:38:47 +0000 (20:38 -0400)
committerLanius Trolling <lanius@laniustrolling.dev>
Fri, 12 Apr 2024 00:38:47 +0000 (20:38 -0400)
src/jvmMain/kotlin/info/mechyrdia/lore/asset_caching.kt

index 6052dc1f033cb385d25dd9dba1a20c72e34ffa83..4db2e64bf4820c71aaf3bf15e022b8276c5ec446 100644 (file)
@@ -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<StoragePath>("Mechyrdia.StoragePath")
 
 abstract class FileDependentCache<T : Any> {
        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() }
                        }