LRU 算法
缓存淘汰算法
FIFO
class Node<K, V> {
K key;
V value;
Node<K, V> prev;
Node<K, V> next;
public Node(Node<K, V> prev, Node<K, V> next, K key, V value) {
this.key = key;
this.value = value;
this.prev = prev;
this.next = next;
}
}
public class FIFOCache<K, V> {
Node<K, V> head;
Node<K, V> tail;
Map<K, Node<K, V>> datas;
int capacity;
int size;
public FIFOCache(int capacity) {
this.capacity = capacity;
size = 0;
datas = new HashMap<>();
head = new Node<>(null, null, null, null);
tail = head;
}
public V get(K key) {
Node<K, V> node = datas.get(key);
if(node == null)
return null;
else return node.value;
}
public void put(K key, V value) {
if(datas.containsKey(key))
return;
Node<K, V> node = new Node<>(tail, null, key, value);
tail.next = node;
datas.put(key, node);
tail = node;
if(size == capacity) {
// 先从map中删除,再删链表中元素
datas.remove(head.key);
head = head.next;
head.prev = null;
} else if(size < capacity) {
if(size == 0)
head = node;
size++;
}
}
public static void main(String[] args) {
FIFOCache<Integer, Integer> cache = new FIFOCache<>(2);
cache.put(1, 1);
cache.put(2, 2);
System.out.println(cache.get(1));
cache.put(3, 3);
System.out.println(cache.get(1));
System.out.println(cache.get(3));
}
}
/* output
1
null
3 */LFU(Least frequently used)
LRU(Least recently used)
LRU 缓存简单实现
Last updated