Skip to content

Commit

Permalink
Replace LruCache with cache4k
Browse files Browse the repository at this point in the history
  • Loading branch information
saket committed Jan 2, 2024
1 parent 349b57c commit 3d818db
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 156 deletions.
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ dokka = "1.9.10"
ksoup = "0.1.2" # https://github.com/fleeksoft/ksoup/releases
mavenPublish = "0.25.3"
ktor = "2.3.7"
assertk = "0.28.0"
assertk = "0.28.0" # https://github.com/willowtreeapps/assertk/releases
cache4k = "0.12.0" # https://github.com/ReactiveCircus/cache4k/releases

[plugins]
androidLibrary = { id = "com.android.library", version.ref = "agp" }
Expand All @@ -25,3 +26,4 @@ ktor-client-darwin = { module = "io.ktor:ktor-client-darwin", version.ref = "kto
ktor-client-mock = { module = "io.ktor:ktor-client-mock", version.ref = "ktor" }
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
assertk = { module = "com.willowtreeapps.assertk:assertk", version.ref = "assertk" }
cache4k = { module = "io.github.reactivecircus.cache4k:cache4k", version.ref = "cache4k" }
1 change: 1 addition & 0 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ kotlin {
commonMain.dependencies {
implementation(libs.ktor.client.core)
implementation(libs.ksoup)
implementation(libs.cache4k)
}
commonTest.dependencies {
implementation(libs.ktor.client.mock)
Expand Down
152 changes: 0 additions & 152 deletions library/src/commonMain/kotlin/me/saket/unfurl/internal/LruCache.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package me.saket.unfurl.internal

import io.github.reactivecircus.cache4k.Cache
import me.saket.unfurl.internal.NullableLruCache.Optional.None
import me.saket.unfurl.internal.NullableLruCache.Optional.Some
import kotlin.time.Duration.Companion.hours

internal class NullableLruCache<K : Any, V>(maxSize: Int) {
private val delegate = LruCache<K, Optional<V>>(maxSize)
private val delegate = Cache.Builder<K, Optional<V>>()
.maximumCacheSize(maxSize.toLong())
.expireAfterAccess(24.hours)
.build()

inline fun computeIfAbsent(key: K, create: () -> V?): V? {
return when (val cached = delegate[key]) {
return when (val cached = delegate.get(key)) {
is Some -> cached.value
is None -> null
null -> create().also {
delegate[key] = if (it == null) None else Some(it)
delegate.put(key, if (it == null) None else Some(it))
}
}
}
Expand Down

0 comments on commit 3d818db

Please sign in to comment.