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

com.blr19c.falowp.bot.system.cache.CacheReference.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC2
Show newest version
package com.blr19c.falowp.bot.system.cache

import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import kotlinx.coroutines.runBlocking
import kotlin.reflect.KProperty
import kotlin.time.Duration
import kotlin.time.toJavaDuration

/**
 * 缓存
 */
class CacheReference(
    duration: Duration,
    private val block: suspend () -> T,
) {
    private val cache = CacheBuilder.newBuilder()
        .expireAfterWrite(duration.toJavaDuration())
        .build(CacheLoader.from { _ -> runBlocking { block.invoke() } })

    operator fun getValue(thisRef: Any, property: KProperty<*>): T {
        return cache.get("onlyKey")
    }

    fun refresh() {
        return cache.refresh("onlyKey")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy