aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorhannibal2 <24389977+hannibal00212@users.noreply.github.com>2024-09-25 02:39:14 +0200
committerhannibal2 <24389977+hannibal00212@users.noreply.github.com>2024-09-25 02:39:14 +0200
commit25cb5c1d8afb37447c7be91d6b1a6a10b32b541b (patch)
tree616a5fc148a9c654d449516c7bb63768abfebf70 /src/main
parent0a06b9d9b9d66228b588d452be25945b8f7d8935 (diff)
downloadskyhanni-25cb5c1d8afb37447c7be91d6b1a6a10b32b541b.tar.gz
skyhanni-25cb5c1d8afb37447c7be91d6b1a6a10b32b541b.tar.bz2
skyhanni-25cb5c1d8afb37447c7be91d6b1a6a10b32b541b.zip
removed cacheLock from TimeLimitedCache
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/TimeLimitedCache.kt33
1 files changed, 3 insertions, 30 deletions
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<K : Any, V : Any>(
@@ -11,19 +10,8 @@ class TimeLimitedCache<K : Any, V : Any>(
private val removalListener: (K?, V?) -> Unit = { _, _ -> },
) : Iterable<Map.Entry<K, V>> {
- 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<K, V>()
+ private val cache = CacheBuilder.newBuilder().expireAfterWrite(expireAfterWrite.inWholeMilliseconds, TimeUnit.MILLISECONDS)
+ .removalListener { removalListener(it.key, it.value) }.build<K, V>()
// TODO IntelliJ cant replace this, find another way?
// @Deprecated("outdated", ReplaceWith("[key] = value"))
@@ -59,22 +47,7 @@ class TimeLimitedCache<K : Any, V : Any>(
*
* @return A read-only view of the cache's underlying map.
*/
- private fun getMap(): ConcurrentMap<K, V> {
- val asMap: ConcurrentMap<K, V>
-
- // 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<K, V> = cache.asMap()
fun containsKey(key: K): Boolean = cache.getIfPresent(key) != null