Merge pull request #444 from kaykong/patch-1

Update 缓存.md
This commit is contained in:
郑永川 2018-10-16 21:56:13 +08:00 committed by GitHub
commit 29c318b846
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -88,12 +88,14 @@ public class LRU<K, V> implements Iterable<K> {
public void put(K key, V value) { public void put(K key, V value) {
Node node;
if (map.containsKey(key)) { if (map.containsKey(key)) {
Node node = map.get(key); node = map.get(key);
unlink(node); unlink(node);
} }
if (node == null) {
Node node = new Node(key, value); node = new Node(key, value);
}
map.put(key, node); map.put(key, node);
appendHead(node); appendHead(node);
@ -109,6 +111,9 @@ public class LRU<K, V> implements Iterable<K> {
Node next = node.next; Node next = node.next;
pre.next = next; pre.next = next;
next.pre = pre; next.pre = pre;
node.pre = null;
node.next = null;
} }