diff --git a/notes/缓存.md b/notes/缓存.md index 07a9e866..3a3e8140 100644 --- a/notes/缓存.md +++ b/notes/缓存.md @@ -88,12 +88,14 @@ public class LRU implements Iterable { public void put(K key, V value) { + Node node; if (map.containsKey(key)) { - Node node = map.get(key); + node = map.get(key); unlink(node); } - - Node node = new Node(key, value); + if (node == null) { + node = new Node(key, value); + } map.put(key, node); appendHead(node); @@ -109,6 +111,9 @@ public class LRU implements Iterable { Node next = node.next; pre.next = next; next.pre = pre; + + node.pre = null; + node.next = null; }