cache disk blocks in an LRU cache
Improves read access by factor 4 for small trees.
This commit is contained in:
35
pdb-utils/src/main/java/org/lucares/utils/cache/LRUCache.java
vendored
Normal file
35
pdb-utils/src/main/java/org/lucares/utils/cache/LRUCache.java
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
package org.lucares.utils.cache;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class LRUCache<K, V> {
|
||||
private final LinkedHashMap<K, V> cache;
|
||||
|
||||
public LRUCache(final int maxEntries) {
|
||||
this.cache = new LinkedHashMap<>(16, 0.75f, true) {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected boolean removeEldestEntry(final Map.Entry<K, V> eldest) {
|
||||
return size() > maxEntries;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public V put(final K key, final V value) {
|
||||
return cache.put(key, value);
|
||||
}
|
||||
|
||||
public V get(final K key) {
|
||||
return cache.get(key);
|
||||
}
|
||||
|
||||
public V remove(final K key) {
|
||||
return cache.remove(key);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return cache.size();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user