track the cache hit rate

This commit is contained in:
2020-10-04 17:16:52 +02:00
parent d4d3d01405
commit ea7ba042df

View File

@@ -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;
}
}