diff --git a/src/commonMain/kotlin/centre/sciprog/maps/LruCache.kt b/src/commonMain/kotlin/centre/sciprog/maps/LruCache.kt index 9619eb0..3a2f0a9 100644 --- a/src/commonMain/kotlin/centre/sciprog/maps/LruCache.kt +++ b/src/commonMain/kotlin/centre/sciprog/maps/LruCache.kt @@ -6,16 +6,11 @@ import kotlin.jvm.Synchronized class LruCache( private var capacity: Int, ) { - - private val cache = mutableMapOf() - private val order = mutableListOf() + private val cache = linkedMapOf() @Synchronized fun getCache() = cache.toMap() - @Synchronized - fun getOrder() = order.toList() - @Synchronized fun put(key: K, value: V) = internalPut(key, value) @@ -25,7 +20,6 @@ class LruCache( @Synchronized fun remove(key: K) { cache.remove(key) - order.remove(key) } @Synchronized @@ -37,25 +31,23 @@ class LruCache( @Synchronized fun clear(newCapacity: Int? = null) { cache.clear() - order.clear() capacity = newCapacity ?: capacity } private fun internalGet(key: K): V? { val value = cache[key] if (value != null) { - order.remove(key) - order.add(key) + cache.remove(key) + cache[key] = value } return value } private fun internalPut(key: K, value: V) { if (cache.size >= capacity) { - cache.remove(order.removeAt(0)) + cache.remove(cache.iterator().next().key) } cache[key] = value - order.add(key) } }