LRU緩存淘汰算法:
LRU是最近最少使用策略的縮寫,是根據數據的歷史訪問記錄來進行淘汰數據,其核心思想是“如果數據最近被訪問過,那麼將來被訪問的機率也更高”。
利用鏈表實現LRU原理:
將Cache的所有位置都用雙鏈表連接起來,當一個位置被命中之後,通過調整鏈表的指向,將該位置調整到鏈表頭的位置,新加入的Cache直接加到鏈表頭中。
這樣,在多次進行Cache操作後,最近被命中的,就會被向鏈表頭方向移動,而沒有命中的,而想鏈表後面移動,鏈表尾則表示最近最少使用的Cache。
當需要替換內容時候,鏈表的最後位置就是最少被命中的位置,我們只需要淘汰鏈表最後的部分即可。
利用鏈表和hashpmap實現
/**
* @author :dongshuo
* @date : 2018/12/10 14:27
* @desc : 鏈表+hashmap實現的
*/
public class LRUCache1<K,V> {
private final int MAX_CACHE_SIZE;//最大的緩存大小
private Entry first; //頭元素
private Entry last;//尾元素
private HashMap<K, Entry<K, V>> hashMap;//用來保存的hashmap
/**
* 初始化 緩存大小和對象
* @param cacheSize
*/
public LRUCache1(int cacheSize) {
MAX_CACHE_SIZE = cacheSize;
hashMap = new HashMap<K, Entry<K, V>>();
}
/**
* 存入k,v
* @param key
* @param value
*/
public void put(K key, V value) {
//1首先判斷是否存在該值
Entry entry = getEntry(key);
//如果爲空
if (entry == null) {
if (hashMap.size() >= MAX_CACHE_SIZE) {
hashMap.remove(last.key);
removeLast();
}
entry = new Entry();
entry.key = key;
}
entry.value = value;
moveToFirst(entry);
hashMap.put(key, entry);
}
public V get(K key) {
Entry<K, V> entry = getEntry(key);
if (entry == null) return null;
moveToFirst(entry);
return entry.value;
}
public void remove(K key) {
Entry entry = getEntry(key);
if (entry != null) {
if (entry.pre != null) entry.pre.next = entry.next;
if (entry.next != null) entry.next.pre = entry.pre;
if (entry == first) first = entry.next;
if (entry == last) last = entry.pre;
}
hashMap.remove(key);
}
/**
* 將元素移到first
* @param entry
*/
private void moveToFirst(Entry entry) {
if (entry == first) return;//元素已經在first位置了
if (entry.pre != null) entry.pre.next = entry.next;
if (entry.next != null) entry.next.pre = entry.pre;
if (entry == last) last = last.pre;//元素在last位置
if (first == null || last == null) {
first = last = entry;
return;
}
entry.next = first;//元素放到first
first.pre = entry;
first = entry;
entry.pre = null;
}
/**
* 刪除最後一個元素
*/
private void removeLast() {
if (last != null) {
last = last.pre;//last元素被刪除,last前驅作爲最後一個元素
if (last == null) first = null;//如果 first=last情況時
else last.next = null;//last後繼爲null
}
}
/**
* 判斷map是否存在該key,value
* @param key
* @return
*/
private Entry<K, V> getEntry(K key) {
return hashMap.get(key);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Entry entry = first;
while (entry != null) {
sb.append(String.format("%s:%s ", entry.key, entry.value));
entry = entry.next;
}
return sb.toString();
}
/**
* 內部類 鏈表
* @param <K>
* @param <V>
*/
class Entry<K, V> {
public Entry pre;
public Entry next;
public K key;
public V value;
}
}
參考:
https://blog.csdn.net/drdongshiye/article/details/84940451