Kotlin Guava Cache 没有显示正确的 hitCount
Kotlin Guava Cache not showing right hitCount
我正在尝试使用此处所示的 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。
我在这里错过了什么?
您错过了 .recordStats()
来电 CacheBuilder
:
Enable the accumulation of CacheStats
during the operation of the cache. Without this Cache.stats()
will return zero for all statistics.
我正在尝试使用此处所示的 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。
我在这里错过了什么?
您错过了 .recordStats()
来电 CacheBuilder
:
Enable the accumulation of
CacheStats
during the operation of the cache. Without thisCache.stats()
will return zero for all statistics.