【问题标题】:Kotlin Guava Cache not showing right hitCountKotlin Guava Cache 未显示正确的 hitCount
【发布时间】:2019-02-17 20:36:21
【问题描述】:

我正在尝试使用 LRU 缓存,如此处所示 Java time-based map/cache with expiring keys

我的代码:

import com.google.common.cache.CacheBuilder
import java.util.concurrent.TimeUnit

fun main(args: Array<String>) {


val cache = CacheBuilder.newBuilder().maximumSize(100).
    expireAfterAccess(10, TimeUnit.HOURS)
    .build<String, String>()


    cache.put("a", "blah")
    val x = cache.getIfPresent("a")
    cache.stats().also { println(it) }
    println(x)

}

输出:

CacheStats{hitCount=0, missCount=0, loadSuccessCount=0, loadExceptionCount=0, totalLoadTime=0, evictionCount=0}
blah

我原以为 hitCount 是 1,而不是 0。

我在这里缺少什么?

【问题讨论】:

    标签: java caching kotlin guava


    【解决方案1】:

    你错过了.recordStats() 打电话给CacheBuilder

    在缓存操作期间启用CacheStats的累积。如果没有这个Cache.stats(),所有统计信息都将返回零。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      相关资源
      最近更新 更多