auto commit
This commit is contained in:
parent
29c318b846
commit
4435076f73
29
notes/缓存.md
29
notes/缓存.md
@ -88,14 +88,12 @@ public class LRU<K, V> implements Iterable<K> {
|
||||
|
||||
public void put(K key, V value) {
|
||||
|
||||
Node node;
|
||||
if (map.containsKey(key)) {
|
||||
node = map.get(key);
|
||||
Node node = map.get(key);
|
||||
unlink(node);
|
||||
}
|
||||
if (node == null) {
|
||||
node = new Node(key, value);
|
||||
}
|
||||
|
||||
Node node = new Node(key, value);
|
||||
map.put(key, node);
|
||||
appendHead(node);
|
||||
|
||||
@ -107,28 +105,38 @@ public class LRU<K, V> implements Iterable<K> {
|
||||
|
||||
|
||||
private void unlink(Node node) {
|
||||
|
||||
Node pre = node.pre;
|
||||
Node next = node.next;
|
||||
|
||||
pre.next = next;
|
||||
next.pre = pre;
|
||||
|
||||
|
||||
node.pre = null;
|
||||
node.next = null;
|
||||
}
|
||||
|
||||
|
||||
private void appendHead(Node node) {
|
||||
node.next = head.next;
|
||||
node.next.pre = node;
|
||||
Node next = head.next;
|
||||
node.next = next;
|
||||
next.pre = node;
|
||||
node.pre = head;
|
||||
head.next = node;
|
||||
}
|
||||
|
||||
|
||||
private Node removeTail() {
|
||||
|
||||
Node node = tail.pre;
|
||||
tail.pre = node.pre;
|
||||
node.pre.next = tail;
|
||||
|
||||
Node pre = node.pre;
|
||||
tail.pre = pre;
|
||||
pre.next = tail;
|
||||
|
||||
node.pre = null;
|
||||
node.next = null;
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
@ -138,6 +146,7 @@ public class LRU<K, V> implements Iterable<K> {
|
||||
|
||||
return new Iterator<K>() {
|
||||
private Node cur = head.next;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return cur != tail;
|
||||
|
Loading…
x
Reference in New Issue
Block a user