From ca60178ed308cdfbf5e3b035ad4674823d25b3a2 Mon Sep 17 00:00:00 2001 From: "Kenneth J. Shackleton" Date: Sat, 22 Jun 2024 17:09:53 +0100 Subject: [PATCH] Simpler get. --- .../bloomberg/selekt/cache/CommonLruCache.kt | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/selekt-java/src/main/kotlin/com/bloomberg/selekt/cache/CommonLruCache.kt b/selekt-java/src/main/kotlin/com/bloomberg/selekt/cache/CommonLruCache.kt index 4b64769e0b..ac15e06428 100644 --- a/selekt-java/src/main/kotlin/com/bloomberg/selekt/cache/CommonLruCache.kt +++ b/selekt-java/src/main/kotlin/com/bloomberg/selekt/cache/CommonLruCache.kt @@ -46,21 +46,18 @@ class CommonLruCache( } 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 {