package at.hannibal2.skyhanni.utils import com.google.common.cache.CacheBuilder import java.util.concurrent.TimeUnit import kotlin.time.Duration class TimeLimitedCache( expireAfterWrite: Duration, private val removalListener: (K?, V?) -> Unit = { _, _ -> }, ): Iterable> { private val cache = CacheBuilder.newBuilder() .expireAfterWrite(expireAfterWrite.inWholeMilliseconds, TimeUnit.MILLISECONDS) .removalListener { removalListener(it.key, it.value) } .build() fun put(key: K, value: V) = cache.put(key, value) fun getOrNull(key: K): V? = cache.getIfPresent(key) fun getOrPut(key: K, defaultValue: () -> V) = getOrNull(key) ?: defaultValue().also { put(key, it) } fun clear() = cache.invalidateAll() fun entries(): Set> = cache.asMap().entries fun values(): Collection = cache.asMap().values fun keys(): Set = cache.asMap().keys fun containsKey(key: K): Boolean = cache.getIfPresent(key) != null override fun iterator(): Iterator> = entries().iterator() }