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( expireAfterWrite: Duration, 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() // TODO IntelliJ cant replace this, find another way? // @Deprecated("outdated", ReplaceWith("[key] = value")) @Deprecated("outdated", ReplaceWith("set(key, value)")) fun put(key: K, value: V) = set(key, value) fun getOrNull(key: K): V? = cache.getIfPresent(key) fun getOrPut(key: K, defaultValue: () -> V) = getOrNull(key) ?: defaultValue().also { set(key, it) } fun clear() = cache.invalidateAll() fun remove(key: K) = cache.invalidate(key) fun entries(): Set> = getMap().entries fun values(): Collection = getMap().values fun keys(): Set = getMap().keys /** * Modifications to the returned map are not supported and may lead to unexpected behavior. * This method is intended for read-only operations such as iteration or retrieval of values. * * @return A read-only view of the cache's underlying map. */ private fun getMap(): ConcurrentMap { val asMap: ConcurrentMap cacheLock.readLock().lock() try { asMap = cache.asMap() } finally { cacheLock.readLock().unlock() } return asMap } fun containsKey(key: K): Boolean = cache.getIfPresent(key) != null override fun iterator(): Iterator> = entries().iterator() operator fun set(key: K, value: V) { cache.put(key, value) } }