Skip to content

Commit

Permalink
Simpler get.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethshackleton committed Jun 22, 2024
1 parent 5a57eee commit ca60178
Showing 1 changed file with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,18 @@ class CommonLruCache<T : Any>(
}

inline fun get(key: String, supplier: () -> T): T {
cache?.let {
return it.get(key) {
supplier().also { value ->
if (it.shouldTransform()) {
// Adding another entry to the cache will necessitate the removal of the
// least recently used entry first to honour our maximum size constraint.
// For the implementation of the store currently assigned, this is an O(N)
// operation. We transform to an O(1) implementation.
transform()
linkedCache!!.store.put(key, value)
}
return cache?.get(key) {
supplier().also { value ->
if (cache!!.shouldTransform()) {
// Adding another entry to the cache will necessitate the removal of the
// least recently used entry first to honour our maximum size constraint.
// For the implementation of the store currently assigned, this is an O(N)
// operation. We transform to an O(1) implementation.
transform()
linkedCache!!.store.put(key, value)
}
}
}
return linkedCache!!.get(key, supplier)
} ?: linkedCache!!.get(key, supplier)
}

fun containsKey(key: String): Boolean {
Expand Down

0 comments on commit ca60178

Please sign in to comment.