LRU(Least recently used,最近最少使用)算法根據數據的歷史訪問記錄來進行淘汰數據,其核心思想是“如果數據最近被訪問過,那么將來被訪問的幾率也更高”。
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
static class LRULinkedHashMap<K,V> extends LinkedHashMap<K,V> {
//定義緩存的容量
private int capacity;
//帶參數的構造器
LRULinkedHashMap(int capacity){
//如果accessOrder為true的話,則會把訪問過的元素放在鏈表后面,放置順序是訪問的順序
//如果accessOrder為flase的話,則按插入順序來遍歷
super(16,0.75f,true);
//傳入指定的緩存最大容量
this.capacity=capacity;
}
//實現LRU的關鍵方法,如果map里面的元素個數大於了緩存最大容量,則刪除鏈表的頂端元素
@Override
public boolean removeEldestEntry(Map.Entry<K, V> eldest){
return size()>capacity;
}
}
//test
public static void main(String[] args) {
LRULinkedHashMap<String, Integer> testCache = new LRULinkedHashMap<>(3);
testCache.put("A", 1);
testCache.put("B", 2);
testCache.put("C", 3);
System.out.println(testCache.get("B"));
System.out.println(testCache.get("A"));
testCache.put("D", 4);
System.out.println(testCache.get("D"));
System.out.println(testCache.get("C"));
}
}
API的使用:
- 首先是LinkedHashMap的構造器:
//如果accessOrder為true的話,則會把訪問過的元素放在鏈表后面,放置順序是訪問的順序
public LinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder) {
super(initialCapacity, loadFactor);
this.accessOrder = accessOrder;
}
- 重寫removeEldestEntry方法
//removeEldestEntry方法會在afterNodeInsertion中調用
//在每次put操作末尾會調用afterNodeInsertion方法。可以利用此方法刪除鏈表的頂端元素。
void afterNodeInsertion(boolean evict) { // possibly remove eldest
LinkedHashMap.Entry<K,V> first;
if (evict && (first = head) != null && removeEldestEntry(first)) {
K key = first.key;
removeNode(hash(key), key, null, false, true);
}
}
