Reputation: 1653
I'm using Guava Cache lib ,and i wanna check the statistics of the cache here is my code :
refreshCache = CacheBuilder.newBuilder()
.refreshAfterWrite(1, TimeUnit.MINUTES)
.maximumSize(5)
.recordStats()
.build(
new CacheLoader<String, Map<String,Object>>() {
public Map<String,Object> load(String key) throws Exception {
loader();
}
});
stats = refreshCache.stats();
delta = refreshCache.stats()
.minus(stats);
}
logger.info("Stats:{}",stats);
logger.info("delta:{}",delta);
i've called recordeStats()
to active the Stats , but when i print the stats , it's all 0 .
Upvotes: 10
Views: 6211
Reputation: 2835
You forgot to activate the cache recording by adding recordStats()
to CacheBuilder.newBuilder()
.
Upvotes: 44
Reputation: 1732
You can not instantiate stats(). It is a factory which copies over the cache values to a final statistic. So please use logger.info(refreshCache.stats()) during runtime.
Upvotes: 2
Reputation: 7371
According to JavaDoc:
Cache statistics are incremented according to the following rules:
A lookup is specifically defined as an invocation of one of the methods LoadingCache.get(Object), LoadingCache.getUnchecked(Object), Cache.get(Object, Callable), or LoadingCache.getAll(Iterable).
Upvotes: 7
Reputation: 110046
In your code, you aren't actually using the cache before getting its stats... why is it surprising that the stats are all 0?
Upvotes: 2