-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRUCache.java.eml
51 lines (39 loc) · 1016 Bytes
/
LRUCache.java.eml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ${domain.namespace};
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class LRUCache<K, V> {
private final LinkedHashMap<K, V> cache;
public LRUCache(int maxCacheSize) {
cache = new LinkedHashMap<K, V>(maxCacheSize, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > maxCacheSize;
}
};
}
public boolean containsKey(K key) {
return cache.containsKey(key);
}
public V get(K key) {
return cache.get(key);
}
public void put(K key, V value) {
cache.put(key, value);
}
public int size() {
return cache.size();
}
public Set<K> getKeys() {
return cache.keySet();
}
public void clear() {
cache.clear();
}
public void putAll(Map<K, V> map) {
cache.putAll(map);
}
public void invalidate(K key) {
cache.remove(key);
}
}