Skip to content

Commit

Permalink
use cache4k as LruCache backing that is thread safe. fixes #75
Browse files Browse the repository at this point in the history
  • Loading branch information
luca992 committed Nov 24, 2023
1 parent 9aa848d commit dd45d32
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 218 deletions.
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ com-android-application = { id = "com.android.application", version.ref = "agp"

[versions]

cache4k = "0.12.0"
kotlin = "1.9.21"
agp = "8.1.4"

Expand All @@ -30,7 +31,7 @@ batik = "1.17"

[libraries]

androidx-startup = { module = "androidx.startup:startup-runtime", version.ref = "startup-runtime" }
cache4k = { module = "io.github.reactivecircus.cache4k:cache4k", version.ref = "cache4k" }
okio = { module = "com.squareup.okio:okio", version.ref = "okio" }
okio-fakefilesystem = { module = "com.squareup.okio:okio-fakefilesystem", version.ref = "okio" }

Expand All @@ -51,6 +52,7 @@ dev-icerock-moko-resources-test = { module = "dev.icerock.moko:resources-test",

pdvrieze-xmlutil-serialization = { module = "io.github.pdvrieze.xmlutil:serialization", version.ref = "xmlutil" }

androidx-startup = { module = "androidx.startup:startup-runtime", version.ref = "startup-runtime" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activity-compose" }
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
google-android-material = { module = "com.google.android.material:material", version.ref = "material" }
Expand Down
1 change: 1 addition & 0 deletions kamel-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ kotlin {
implementation(libs.kotlinx.coroutines.core)
implementation(libs.ktor.client.core)
implementation(libs.okio)
implementation(libs.cache4k)
}
}

Expand Down

This file was deleted.

26 changes: 25 additions & 1 deletion kamel-core/src/commonMain/kotlin/io/kamel/core/cache/LruCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,28 @@ package io.kamel.core.cache
/**
* Cache implementation which evicts items using an LRU algorithm.
*/
internal expect class LruCache<K, V>(maxSize: Int) : Cache<K, V>
internal class LruCache<K : Any, V : Any>(override val maxSize: Int) : Cache<K, V> {

private val cache: io.github.reactivecircus.cache4k.Cache<K, V> =
io.github.reactivecircus.cache4k.Cache.Builder<K, V>()
.maximumCacheSize(maxSize.toLong())
.build()

override val size: Int
get() = cache.asMap().size

init {
require(maxSize >= 0) { "Cache max size must be positive number" }
}

override fun get(key: K): V? = cache.get(key)

override fun set(key: K, value: V) = cache.put(key, value)

override fun remove(key: K): Boolean {
cache.invalidate(key)
return true
}

override fun clear(): Unit = cache.invalidateAll()
}
26 changes: 0 additions & 26 deletions kamel-core/src/nonJvmMain/kotlin/io/kamel/core/cache/LruCache.kt

This file was deleted.

This file was deleted.

0 comments on commit dd45d32

Please sign in to comment.