ru.hnau.jutils.cache.AsyncAutoPossibleCache.kt Maven / Gradle / Ivy
package ru.hnau.jutils.cache
import ru.hnau.jutils.finisher.Finisher
import ru.hnau.jutils.finisher.awaitError
import ru.hnau.jutils.possible.Possible
open class AsyncAutoPossibleCache(
private val getter: (K) -> Finisher>,
capacity: Int
) : Cache>>(
capacity
) {
operator fun get(key: K) = getValue(key) ?: run {
synchronized(this@AsyncAutoPossibleCache) {
val finisher = getter.invoke(key)
putValue(key, finisher)
finisher.awaitError {
removeValue(key)
}
return@synchronized finisher
}
}
}