Skip to content

Commit 7bc6fec

Browse files
authored
Improve LRUCache implementation.
1 parent ad89c68 commit 7bc6fec

File tree

3 files changed

+16
-69
lines changed

3 files changed

+16
-69
lines changed

pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@
158158
<option>-keep public class com.github.underscore.lodash.*$ParseException { *; }</option>
159159
<option>-keep public class com.github.underscore.lodash.*$FetchResponse { *; }</option>
160160
<option>-keep public class com.github.underscore.lodash.*$LRUCache { *; }</option>
161-
<option>-keep public class com.github.underscore.lodash.*$Node { *; }</option>
162161
<option>-keepclassmembers class * { *** newArrayList(); *** newLinkedHashSet(); *** newHashSet(java.lang.Iterable); *** newLinkedHashMap(); }</option>
163162
<option>-dontnote com.github.underscore.*$ClassForName</option>
164163
</options>

src/main/java/com/github/underscore/lodash/$.java

Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,79 +2827,27 @@ public List<String> words() {
28272827
}
28282828

28292829
public static class LRUCache<K, V> {
2830-
private int capacity;
2831-
private Map<K, Node<K, V>> map = new HashMap<K, Node<K, V>>();
2832-
private Node head;
2833-
private Node end;
2830+
private static final boolean SORT_BY_ACCESS = true;
2831+
private static final float LOAD_FACTOR = 0.75F;
2832+
private final LinkedHashMap<K, V> lruCacheMap;
2833+
private final int capacity;
28342834

28352835
public LRUCache(int capacity) {
28362836
this.capacity = capacity;
2837+
this.lruCacheMap = new LinkedHashMap<K, V>(capacity, LOAD_FACTOR, SORT_BY_ACCESS);
28372838
}
28382839

28392840
public V get(K key) {
2840-
if (map.containsKey(key)) {
2841-
Node<K, V> node = map.get(key);
2842-
remove(node);
2843-
setHead(node);
2844-
return node.value;
2845-
}
2846-
return null;
2847-
}
2848-
2849-
public void remove(Node node) {
2850-
if (node.pre != null) {
2851-
node.pre.next = node.next;
2852-
} else {
2853-
head = node.next;
2854-
}
2855-
if (node.next != null) {
2856-
node.next.pre = node.pre;
2857-
} else {
2858-
end = node.pre;
2859-
}
2841+
return lruCacheMap.get(key);
28602842
}
28612843

2862-
public void setHead(Node node) {
2863-
node.next = head;
2864-
node.pre = null;
2865-
if (head != null) {
2866-
head.pre = node;
2844+
public void put(K key, V value) {
2845+
if (lruCacheMap.containsKey(key)) {
2846+
lruCacheMap.remove(key);
2847+
} else if (lruCacheMap.size() >= capacity) {
2848+
lruCacheMap.remove(lruCacheMap.keySet().iterator().next());
28672849
}
2868-
head = node;
2869-
if (end == null) {
2870-
end = head;
2871-
}
2872-
}
2873-
2874-
public void set(K key, V value) {
2875-
if (map.containsKey(key)) {
2876-
Node<K, V> old = map.get(key);
2877-
old.value = value;
2878-
remove(old);
2879-
setHead(old);
2880-
} else {
2881-
Node<K, V> created = new Node<K, V>(key, value);
2882-
if (map.size() >= capacity) {
2883-
map.remove(end.key);
2884-
remove(end);
2885-
setHead(created);
2886-
} else {
2887-
setHead(created);
2888-
}
2889-
map.put(key, created);
2890-
}
2891-
}
2892-
}
2893-
2894-
public static class Node<K, V> {
2895-
private K key;
2896-
private V value;
2897-
private Node pre;
2898-
private Node next;
2899-
2900-
public Node(K key, V value) {
2901-
this.key = key;
2902-
this.value = value;
2850+
lruCacheMap.put(key, value);
29032851
}
29042852
}
29052853

src/test/java/com/github/underscore/lodash/MathTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ public void sumOfInt() {
199199
public void createLRUCache() {
200200
new $.LRUCache<Integer, String>(0);
201201
$.LRUCache<Integer, String> cache = $.createLRUCache(2);
202-
cache.set(0, "Value 0");
202+
cache.put(0, "Value 0");
203203
assertEquals("Value 0", cache.get(0));
204204
assertNull(cache.get(1));
205-
cache.set(1, "Value 1");
205+
cache.put(1, "Value 1");
206206
assertEquals("Value 1", cache.get(1));
207-
cache.set(1, "Value 1+");
207+
cache.put(1, "Value 1+");
208208
assertEquals("Value 1+", cache.get(1));
209-
cache.set(2, "Value 2");
209+
cache.put(2, "Value 2");
210210
assertEquals("Value 2", cache.get(2));
211211
}
212212

0 commit comments

Comments
 (0)