All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.hnau.jutils.cache.AsyncAutoPossibleCache.kt Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
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
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy