From 25cb5c1d8afb37447c7be91d6b1a6a10b32b541b Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Wed, 25 Sep 2024 02:39:14 +0200 Subject: removed cacheLock from TimeLimitedCache --- .../hannibal2/skyhanni/utils/TimeLimitedCache.kt | 33 ++-------------------- 1 file changed, 3 insertions(+), 30 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt index a88db58e1..c06c73edd 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt @@ -3,7 +3,6 @@ package at.hannibal2.skyhanni.utils import com.google.common.cache.CacheBuilder import java.util.concurrent.ConcurrentMap import java.util.concurrent.TimeUnit -import java.util.concurrent.locks.ReentrantReadWriteLock import kotlin.time.Duration class TimeLimitedCache( @@ -11,19 +10,8 @@ class TimeLimitedCache( private val removalListener: (K?, V?) -> Unit = { _, _ -> }, ) : Iterable> { - private val cacheLock = ReentrantReadWriteLock() - - private val cache = CacheBuilder.newBuilder() - .expireAfterWrite(expireAfterWrite.inWholeMilliseconds, TimeUnit.MILLISECONDS) - .removalListener { - cacheLock.writeLock().lock() - try { - removalListener(it.key, it.value) - } finally { - cacheLock.writeLock().unlock() - } - } - .build() + private val cache = CacheBuilder.newBuilder().expireAfterWrite(expireAfterWrite.inWholeMilliseconds, TimeUnit.MILLISECONDS) + .removalListener { removalListener(it.key, it.value) }.build() // TODO IntelliJ cant replace this, find another way? // @Deprecated("outdated", ReplaceWith("[key] = value")) @@ -59,22 +47,7 @@ class TimeLimitedCache( * * @return A read-only view of the cache's underlying map. */ - private fun getMap(): ConcurrentMap { - val asMap: ConcurrentMap - - // TODO: the returned map view here is live and is updated as the underlying cache is. - // the lock here is relatively pointless. i don't know why it was put here, but i - // can't imagine a good reason, since every access to the returned map would also - // need to be guarded by the same lock to be effective. - cacheLock.readLock().lock() - try { - asMap = cache.asMap() - } finally { - cacheLock.readLock().unlock() - } - - return asMap - } + private fun getMap(): ConcurrentMap = cache.asMap() fun containsKey(key: K): Boolean = cache.getIfPresent(key) != null -- cgit