Stats
用來統計緩存命中情況,包括命中數量hitCount
,未命中數量missCount
及重新加載的次數reloadCount
.
/**
* 緩存統計信息,含命中次數,未命中次數及命中率。
* @author zxm
* @since 2018.02.01
*/
public final class Stats {
private AtomicLong hitCount = new AtomicLong();
private AtomicLong missCount = new AtomicLong();
private AtomicLong reloadCount = new AtomicLong();
...
}
具體使用時可以通過CacheBuilder開啓,
public CacheBuilder<K,V> stats(){
this.stats = new Stats();
return this;
}
開啓後,在get緩存對象的時候,會統計命中情況:
/**
* throw exception when value is null
*
* @param key
* @return V
* @throws UnCheckNullException
* @throws LoadingFailException
*/
public V get(Object key) throws LoadingFailException {
CacheObject<K, V> object = delegate.get(key);
if (object != null) {
if(stats!=null){
stats.hit();
}
object.setLastAccessTime(System.currentTimeMillis());
return object.getValue();
} else if(loader != null){
V v;
try {
v = loader.load((K) key);
} catch (Exception e) {
throw new LoadingFailException("unable to load cache object");
}
if(stats != null){
if(v == null){
stats.miss();
} else {
stats.reload();
}
}
super.put((K) key, v);
object = delegate.get(key);
object.setLastAccessTime(System.currentTimeMillis());
return object.getValue();
}
throw new UnCheckNullException("cache object value can not is null");
}
查詢緩存命中情況時可以調用Cache.stat()方法:
public String stats(){
return stats.toString();
}