1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.turbo.lru;
15
16 import java.util.ArrayList;
17 import java.util.LinkedHashMap;
18 import java.util.List;
19 import java.util.Map;
20
21
22
23
24
25
26
27
28
29 public class X_LRUCache<K, V> extends LinkedHashMap<K, V> {
30 private static final long serialVersionUID = -6592964689843698200L;
31
32 final int cacheSize;
33
34 public X_LRUCache(int cacheSize) {
35 super((int) (cacheSize * (4.0f / 3)), 0.75f, true);
36 if (cacheSize < 1) {
37 throw new IllegalArgumentException("Cache size cannnot be smaller than 1");
38 }
39 this.cacheSize = cacheSize;
40 }
41
42 protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
43 return (size() > cacheSize);
44 }
45
46 List<K> keyList() {
47 ArrayList<K> al = new ArrayList<K>();
48 al.addAll(keySet());
49 return al;
50 }
51 }