track the cache hit rate
This commit is contained in:
@@ -6,6 +6,9 @@ import java.util.Map;
|
||||
public class LRUCache<K, V> {
|
||||
private final LinkedHashMap<K, V> cache;
|
||||
|
||||
private long countGet = 0;
|
||||
private long countGetHits = 0;
|
||||
|
||||
public LRUCache(final int maxEntries) {
|
||||
this.cache = new LinkedHashMap<>(16, 0.75f, true) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@@ -21,7 +24,12 @@ public class LRUCache<K, V> {
|
||||
}
|
||||
|
||||
public V get(final K key) {
|
||||
return cache.get(key);
|
||||
final V result = cache.get(key);
|
||||
countGet++;
|
||||
if (result != null) {
|
||||
countGetHits++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public V remove(final K key) {
|
||||
@@ -32,4 +40,7 @@ public class LRUCache<K, V> {
|
||||
return cache.size();
|
||||
}
|
||||
|
||||
public double cacheHitRate() {
|
||||
return (double) countGetHits / (double) countGet;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user