動手實現一個 LRU cache
阿新 • • 發佈:2018-12-22
前言
LRU 是 LeastRecentlyUsed
的簡寫,字面意思則是 最近最少使用
。通常用於快取的淘汰策略實現,由於快取的記憶體非常寶貴,所以需要根據某種規則來剔除資料保證記憶體不被撐滿。如常用的 Redis 就有以下幾種策略:
策略 | 描述 |
---|---|
volatile-lru | 從已設定過期時間的資料集中挑選最近最少使用的資料淘汰 |
volatile-ttl | 從已設定過期時間的資料集中挑選將要過期的資料淘汰 |
volatile-random | 從已設定過期時間的資料集中任意選擇資料淘汰 |
allkeys-lru | 從所有資料集中挑選最近最少使用的資料淘汰 |
allkeys-random | 從所有資料集中任意選擇資料進行淘汰 |
no-envicition | 禁止驅逐資料 |
實現一
之前也有接觸過一道面試題,大概需求是:
- 實現一個 LRU 快取,當快取資料達到 N 之後需要淘汰掉最近最少使用的資料。
- N 小時之內沒有被訪問的資料也需要淘汰掉。
以下是我的實現:
public class LRUAbstractMap extends java.util.AbstractMap {
private final static Logger LOGGER = LoggerFactory.getLogger(LRUAbstractMap.class);
/**
* 檢查是否超期執行緒
*/
private ExecutorService checkTimePool ;
/**
* map 最大size
*/
private final static int MAX_SIZE = 1024 ;
private final static ArrayBlockingQueue<Node> QUEUE = new ArrayBlockingQueue<>(MAX_SIZE) ;
/**
* 預設大小
*/
private final static int DEFAULT_ARRAY_SIZE =1024 ;
/**
* 陣列長度
*/
private int arraySize ;
/**
* 陣列
*/
private Object[] arrays ;
/**
* 判斷是否停止 flag
*/
private volatile boolean flag = true ;
/**
* 超時時間
*/
private final static Long EXPIRE_TIME = 60 * 60 * 1000L ;
/**
* 整個 Map 的大小
*/
private volatile AtomicInteger size ;
public LRUAbstractMap() {
arraySize = DEFAULT_ARRAY_SIZE;
arrays = new Object[arraySize] ;
//開啟一個執行緒檢查最先放入佇列的值是否超期
executeCheckTime();
}
/**
* 開啟一個執行緒檢查最先放入佇列的值是否超期 設定為守護執行緒
*/
private void executeCheckTime() {
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("check-thread-%d")
.setDaemon(true)
.build();
checkTimePool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(1),namedThreadFactory,new ThreadPoolExecutor.AbortPolicy());
checkTimePool.execute(new CheckTimeThread()) ;
}
@Override
public Set<Entry> entrySet() {
return super.keySet();
}
@Override
public Object put(Object key, Object value) {
int hash = hash(key);
int index = hash % arraySize ;
Node currentNode = (Node) arrays[index] ;
if (currentNode == null){
arrays[index] = new Node(null,null, key, value);
//寫入佇列
QUEUE.offer((Node) arrays[index]) ;
sizeUp();
}else {
Node cNode = currentNode ;
Node nNode = cNode ;
//存在就覆蓋
if (nNode.key == key){
cNode.val = value ;
}
while (nNode.next != null){
//key 存在 就覆蓋 簡單判斷
if (nNode.key == key){
nNode.val = value ;
break ;
}else {
//不存在就新增連結串列
sizeUp();
Node node = new Node(nNode,null,key,value) ;
//寫入佇列
QUEUE.offer(currentNode) ;
cNode.next = node ;
}
nNode = nNode.next ;
}
}
return null ;
}
@Override
public Object get(Object key) {
int hash = hash(key) ;
int index = hash % arraySize ;
Node currentNode = (Node) arrays[index] ;
if (currentNode == null){
return null ;
}
if (currentNode.next == null){
//更新時間
currentNode.setUpdateTime(System.currentTimeMillis());
//沒有衝突
return currentNode ;
}
Node nNode = currentNode ;
while (nNode.next != null){
if (nNode.key == key){
//更新時間
currentNode.setUpdateTime(System.currentTimeMillis());
return nNode ;
}
nNode = nNode.next ;
}
return super.get(key);
}
@Override
public Object remove(Object key) {
int hash = hash(key) ;
int index = hash % arraySize ;
Node currentNode = (Node) arrays[index] ;
if (currentNode == null){
return null ;
}
if (currentNode.key == key){
sizeDown();
arrays[index] = null ;
//移除佇列
QUEUE.poll();
return currentNode ;
}
Node nNode = currentNode ;
while (nNode.next != null){
if (nNode.key == key){
sizeDown();
//在連結串列中找到了 把上一個節點的 next 指向當前節點的下一個節點
nNode.pre.next = nNode.next ;
nNode = null ;
//移除佇列
QUEUE.poll();
return nNode;
}
nNode = nNode.next ;
}
return super.remove(key);
}
/**
* 增加size
*/
private void sizeUp(){
//在put值時候認為裡邊已經有資料了
flag = true ;
if (size == null){
size = new AtomicInteger() ;
}
int size = this.size.incrementAndGet();
if (size >= MAX_SIZE) {
//找到佇列頭的資料
Node node = QUEUE.poll() ;
if (node == null){
throw new RuntimeException("data error") ;
}
//移除該 key
Object key = node.key ;
remove(key) ;
lruCallback() ;
}
}
/**
* 數量減小
*/
private void sizeDown(){
if (QUEUE.size() == 0){
flag = false ;
}
this.size.decrementAndGet() ;
}
@Override
public int size() {
return size.get() ;
}
/**
* 連結串列
*/
private class Node{
private Node next ;
private Node pre ;
private Object key ;
private Object val ;
private Long updateTime ;
public Node(Node pre,Node next, Object key, Object val) {
this.pre = pre ;
this.next = next;
this.key = key;
this.val = val;
this.updateTime = System.currentTimeMillis() ;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
public Long getUpdateTime() {
return updateTime;
}
@Override
public String toString() {
return "Node{" +
"key=" + key +
", val=" + val +
'}';
}
}
/**
* copy HashMap 的 hash 實現
* @param key
* @return
*/
public int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
private void lruCallback(){
LOGGER.debug("lruCallback");
}
private class CheckTimeThread implements Runnable{
@Override
public void run() {
while (flag){
try {
Node node = QUEUE.poll();
if (node == null){
continue ;
}
Long updateTime = node.getUpdateTime() ;
if ((updateTime - System.currentTimeMillis()) >= EXPIRE_TIME){
remove(node.key) ;
}
} catch (Exception e) {
LOGGER.error("InterruptedException");
}
}
}
}
}
感興趣的朋友可以直接從:
https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/actual/LRUAbstractMap.java
下載程式碼本地執行。
程式碼看著比較多,其實實現的思路還是比較簡單:
- 採用了與 HashMap 一樣的儲存資料方式,只是自己手動實現了一個簡易版。
- 內部採用了一個佇列來儲存每次寫入的資料。
- 寫入的時候判斷快取是否大於了閾值 N,如果滿足則根據佇列的 FIFO 特性將佇列頭的資料刪除。因為佇列頭的資料肯定是最先放進去的。
- 再開啟了一個守護執行緒用於判斷最先放進去的資料是否超期(因為就算超期也是最先放進去的資料最有可能滿足超期條件。)
- 設定為守護執行緒可以更好的表明其目的(最壞的情況下,如果是一個使用者執行緒最終有可能導致程式不能正常退出,因為該執行緒一直在執行,守護執行緒則不會有這個情況。)
以上程式碼大體功能滿足了,但是有一個致命問題。
就是最近最少使用沒有滿足,刪除的資料都是最先放入的資料。
不過其中的
putget
流程算是一個簡易的 HashMap 實現,可以對 HashMap 加深一些理解。
實現二
因此如何來實現一個完整的 LRU 快取呢,這次不考慮過期時間的問題。
其實從上一個實現也能想到一些思路:
- 要記錄最近最少使用,那至少需要一個有序的集合來保證寫入的順序。
- 在使用了資料之後能夠更新它的順序。
基於以上兩點很容易想到一個常用的資料結構:連結串列。
- 每次寫入資料時將資料放入連結串列頭結點。
- 使用資料時候將資料移動到頭結點。
- 快取數量超過閾值時移除連結串列尾部資料。
因此有了以下實現:
public class LRUMap<K, V> {
private final Map<K, V> cacheMap = new HashMap<>();
/**
* 最大快取大小
*/
private int cacheSize;
/**
* 節點大小
*/
private int nodeCount;
/**
* 頭結點
*/
private Node<K, V> header;
/**
* 尾結點
*/
private Node<K, V> tailer;
public LRUMap(int cacheSize) {
this.cacheSize = cacheSize;
//頭結點的下一個結點為空
header = new Node<>();
header.next = null;
//尾結點的上一個結點為空
tailer = new Node<>();
tailer.tail = null;
//雙向連結串列 頭結點的上結點指向尾結點
header.tail = tailer;
//尾結點的下結點指向頭結點
tailer.next = header;
}
public void put(K key, V value) {
cacheMap.put(key, value);
//雙向連結串列中新增結點
addNode(key, value);
}
public V get(K key){
Node<K, V> node = getNode(key);
//移動到頭結點
moveToHead(node) ;
return cacheMap.get(key);
}
private void moveToHead(Node<K,V> node){
//如果是最後的一個節點
if (node.tail == null){
node.next.tail = null ;
tailer = node.next ;
nodeCount -- ;
}
//如果是本來就是頭節點 不作處理
if (node.next == null){
return ;
}
//如果處於中間節點
if (node.tail != null && node.next != null){
//它的上一節點指向它的下一節點 也就刪除當前節點
node.tail.next = node.next ;
nodeCount -- ;
}
//最後在頭部增加當前節點
//注意這裡需要重新 new 一個物件,不然原本的node 還有著下面的引用,會造成記憶體溢位。
node = new Node<>(node.getKey(),node.getValue()) ;
addHead(node) ;
}
/**
* 連結串列查詢 效率較低
* @param key
* @return
*/
private Node<K,V> getNode(K key){
Node<K,V> node = tailer ;
while (node != null){
if (node.getKey().equals(key)){
return node ;
}
node = node.next ;
}
return null ;
}
/**
* 寫入頭結點
* @param key
* @param value
*/
private void addNode(K key, V value) {
Node<K, V> node = new Node<>(key, value);
//容量滿了刪除最後一個
if (cacheSize == nodeCount) {
//刪除尾結點
delTail();
}
//寫入頭結點
addHead(node);
}
/**
* 新增頭結點
*
* @param node
*/
private void addHead(Node<K, V> node) {
//寫入頭結點
header.next = node;
node.tail = header;
header = node;
nodeCount++;
//如果寫入的資料大於2個 就將初始化的頭尾結點刪除
if (nodeCount == 2) {
tailer.next.next.tail = null;
tailer = tailer.next.next;
}
}
private void delTail() {
//把尾結點從快取中刪除
cacheMap.remove(tailer.getKey());
//刪除尾結點
tailer.next.tail = null;
tailer = tailer.next;
nodeCount--;
}
private class Node<K, V> {
private K key;
private V value;
Node<K, V> tail;
Node<K, V> next;
public Node(K key, V value) {
this.key = key;
this.value = value;
}
public Node() {
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder() ;
Node<K,V> node = tailer ;
while (node != null){
sb.append(node.getKey()).append(":")
.append(node.getValue())
.append("-->") ;
node = node.next ;
}
return sb.toString();
}
}
原始碼: https://github.com/crossoverJie/Java-Interview/blob/master/src/main/java/com/crossoverjie/actual/LRUMap.java
實際效果,寫入時:
@Test
public void put() throws Exception {
LRUMap<String,Integer> lruMap = new LRUMap(3) ;
lruMap.put("1",1) ;
lruMap.put("2",2) ;
lruMap.put("3",3) ;
System.out.println(lruMap.toString());
lruMap.put("4",4) ;
System.out.println(lruMap.toString());
lruMap.put("5",5) ;
System.out.println(