import java.util.Hashtable; class DLinkedList { String key; //鍵 int value; //值 DLinkedList pre; //雙向鏈表前驅 DLinkedList next; //雙向鏈表后繼 } public class LRUCache { private Hashtable<String,DLinkedList> cache = new Hashtable<String,DLinkedList>(); private int count; private int capacity; private DLinkedList head, tail; public LRUCache(int capacity) { this.count = 0; this.capacity = capacity; head = new DLinkedList(); head.pre = null; tail = new DLinkedList(); tail.next = null; head.next = tail; tail.pre = head; } public int get(String key) { DLinkedList node = cache.get(key); if(node == null) return -1; this.moveToHead(node); return node.value; } public void set(String key,int value) { DLinkedList node = cache.get(key); if(node == null) { DLinkedList newNode = new DLinkedList(); newNode.key = key; newNode.value = value; this.cache.put(key, newNode); this.addNode(newNode); ++count; if(count>capacity) { DLinkedList tail = this.popTail(); this.cache.remove(tail.key); --count; } } else { node.value = value; this.moveToHead(node); } } private void addNode(DLinkedList node) { node.pre = head; node.next = head.next; head.next.pre = node; head.next = node; } private void removeNode(DLinkedList node) { DLinkedList pre = node.pre; DLinkedList next = node.next; pre.next = next; next.pre = pre; } private void moveToHead(DLinkedList node) { this.removeNode(node); this.addNode(node); } private DLinkedList popTail() { DLinkedList res = tail.pre; this.removeNode(res); return res; } @Override public String toString() { StringBuilder sb = new StringBuilder(); DLinkedList node = head; while(node != null){ sb.append(String.format("%s:%s ", node.key,node.value)); node = node.next; } return sb.toString(); } public static void main(String[] args) { LRUCache lru = new LRUCache(3); lru.set("1", 7); System.out.println(lru.toString()); lru.set("2", 0); System.out.println(lru.toString()); lru.set("3", 1); System.out.println(lru.toString()); lru.set("4", 2); System.out.println(lru.toString()); lru.get("2"); System.out.println(lru.toString()); lru.set("5", 3); System.out.println(lru.toString()); lru.get("2"); System.out.println(lru.toString()); lru.set("6", 4); System.out.println(lru.toString()); /* 0ull:0 1:7 null:0 null:0 2:0 1:7 null:0 null:0 3:1 2:0 1:7 null:0 null:0 4:2 3:1 2:0 null:0 null:0 2:0 4:2 3:1 null:0 null:0 5:3 2:0 4:2 null:0 null:0 2:0 5:3 4:2 null:0 null:0 6:4 2:0 5:3 null:0 */ } }
那么如何設計一個LRU緩存,使得放入和移除都是 O(1) 的,我們需要把訪問次序維護起來,但是不能通過內存中的真實排序來反應,有一種方案就是使用雙向鏈表。
整體的設計思路是,可以使用 HashMap 存儲 key,這樣可以做到 save 和 get key的時間都是 O(1),而 HashMap 的 Value 指向雙向鏈表實現的 LRU 的 Node 節點,如圖所示。
LRU 存儲是基於雙向鏈表實現的,下面的圖演示了它的原理。其中 head 代表雙向鏈表的表頭,tail 代表尾部。首先預先設置 LRU 的容量,如果存儲滿了,可以通過 O(1) 的時間淘汰掉雙向鏈表的尾部,每次新增和訪問數據,都可以通過 O(1)的效率把新的節點增加到對頭,或者把已經存在的節點移動到隊頭。
下面展示了,預設大小是 3 的,LRU存儲的在存儲和訪問過程中的變化。為了簡化圖復雜度,圖中沒有展示 HashMap部分的變化,僅僅演示了上圖 LRU 雙向鏈表的變化。我們對這個LRU緩存的操作序列如下:
save("key1", 7)
save("key2", 0)
save("key3", 1)
save("key4", 2)
get("key2")
save("key5", 3)
get("key2")
save("key6", 4)
相應的 LRU 雙向鏈表部分變化如下:
s = save, g = get
總結一下核心操作的步驟:
- save(key, value),首先在 HashMap 找到 Key 對應的節點,如果節點存在,更新節點的值,並把這個節點移動隊頭。如果不存在,需要構造新的節點,並且嘗試把節點塞到隊頭,如果LRU空間不足,則通過 tail 淘汰掉隊尾的節點,同時在 HashMap 中移除 Key。
- get(key),通過 HashMap 找到 LRU 鏈表節點,因為根據LRU 原理,這個節點是最新訪問的,所以要把節點插入到隊頭,然后返回緩存的值。
【https://zhuanlan.zhihu.com/p/34133067】