auto commit
This commit is contained in:
17
notes/缓存.md
17
notes/缓存.md
@ -58,6 +58,7 @@ public class LRU<K, V> implements Iterable<K> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public LRU(int maxSize) {
|
||||
|
||||
this.maxSize = maxSize;
|
||||
@ -70,6 +71,7 @@ public class LRU<K, V> implements Iterable<K> {
|
||||
tail.pre = head;
|
||||
}
|
||||
|
||||
|
||||
public V get(K key) {
|
||||
|
||||
if (!map.containsKey(key)) {
|
||||
@ -83,6 +85,7 @@ public class LRU<K, V> implements Iterable<K> {
|
||||
return node.v;
|
||||
}
|
||||
|
||||
|
||||
public void put(K key, V value) {
|
||||
|
||||
if (map.containsKey(key)) {
|
||||
@ -100,30 +103,34 @@ public class LRU<K, V> implements Iterable<K> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void unlink(Node node) {
|
||||
Node pre = node.pre;
|
||||
node.pre = node.next;
|
||||
node.next = pre;
|
||||
Node next = node.next;
|
||||
pre.next = next;
|
||||
next.pre = pre;
|
||||
}
|
||||
|
||||
|
||||
private void appendHead(Node node) {
|
||||
node.next = head.next;
|
||||
node.pre = head;
|
||||
head.next = node;
|
||||
}
|
||||
|
||||
|
||||
private Node removeTail() {
|
||||
Node node = tail.pre;
|
||||
node.pre = tail;
|
||||
tail.pre = node.pre;
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<K> iterator() {
|
||||
|
||||
return new Iterator<K>() {
|
||||
|
||||
private Node cur = head.next;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return cur != tail;
|
||||
|
Reference in New Issue
Block a user