久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

淺談Java如何實現一個基于LRU時間復雜度為O(1)的緩存

瀏覽:4日期:2022-08-27 15:14:09

LRU:Least Recently Used最近最少使用,當緩存容量不足時,先淘汰最近最少使用的數據。就像JVM垃圾回收一樣,希望將存活的對象移動到內存的一端,然后清除其余空間。

緩存基本操作就是讀、寫、淘汰刪除。

讀操作時間復雜度為O(1)的那就是hash操作了,可以使用HashMap索引 key。

寫操作時間復雜度為O(1),使用鏈表結構,在鏈表的一端插入節點,是可以完成O(1)操作,但是為了配合讀,還要再次將節點放入HashMap中,put操作最優是O(1),最差是O(n)。

不少童鞋就有疑問了,寫入時又使用map進行了put操作,為何緩存不直接使用map?沒錯,首先使用map存儲了節點數據就是采用空間換時間,但是淘汰刪除不好處理,使用map如何去記錄最近最少使用(涉及到時間、頻次問題)。so,使用鏈表可以將活躍節點移動到鏈表的一端,淘汰時直接從另一端進行刪除。

public class LruCache<K,V> {/** 這里簡單點直接初始化了*/ private int capacity = 2; private int size = 0; private HashMap<K,DoubleListNode<K,V>> cache = new HashMap<>(capacity); private DoubleListNode<K,V> lruNode = new DoubleListNode<K, V>(null,null,null,null); private DoubleListNode<K,V> mruNode = new DoubleListNode<K, V>(null,null,null,null); public V get(K key){ DoubleListNode<K,V> target = cache.get(key); if (target == null) { return null; } /** 使用過就移動到右側 */ move2mru(target); return target.value; } public void put(K key,V value){ if(cache.containsKey(key)){ DoubleListNode<K,V> temp = cache.get(key); temp.value = value; /** 使用過就移動到右側 */ move2mru(temp); return; }/** 容量滿了清除左側 */ if(size >= capacity){ evict4lru(); } DoubleListNode<K,V> newNode = new DoubleListNode<>(mruNode,null,key,value); if(size == 0){ lruNode.next = newNode; } mruNode.next = newNode; mruNode = newNode; cache.put(key,newNode); size++; } private void move2mru(DoubleListNode<K,V> newMru){ DoubleListNode<K,V> pre = newMru.pre; DoubleListNode<K,V> next = newMru.next; pre.next = next; newMru.pre = mruNode; mruNode.next = newMru; mruNode = newMru; } private void evict4lru(){ cache.remove(lruNode.next.key); lruNode.next = lruNode.next.next; size--; } public String toString(){ StringBuffer sb = new StringBuffer('lru -> '); DoubleListNode<K,V> temp = lruNode; while(temp!=null){ sb.append(temp.key).append(':').append(temp.value); sb.append(' -> '); temp = temp.next; } sb.append(' -> mru '); return sb.toString(); } public static void main(String[] args) { LruCache<String,String> cache = new LruCache<>(); cache.put('1','1'); System.out.println(cache); cache.get('1'); cache.put('2','2'); System.out.println(cache); cache.put('3','3'); System.out.println(cache); cache.put('4','4'); System.out.println(cache); }}class DoubleListNode<K,V>{ K key; V value; DoubleListNode<K,V> pre; DoubleListNode<K,V> next; public DoubleListNode(K key,V value){ this.key = key; this.value = value; } public DoubleListNode(DoubleListNode<K,V> pre,DoubleListNode<K,V> next,K key,V value){ this.pre = pre; this.next = next; this.key = key; this.value = value; }}

這里使用鏈表,及HashMap完成了基于LRU的緩存,其中HashMap主要用來快速索引key,鏈表用來完成LRU機制。當然尚有許多不足,包括緩存移除remove,緩存ttl,線程安全等。

到此這篇關于淺談Java如何實現一個基于LRU時間復雜度為O(1)的緩存的文章就介紹到這了,更多相關Java基于LRU時間復雜度為O(1)的緩存內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 鹤峰县| 林州市| 韶关市| 绥宁县| 唐山市| 兰溪市| 巫溪县| 德钦县| 合川市| 宜宾市| 平顶山市| 济阳县| 福建省| 沙湾县| 叙永县| 林甸县| 台中县| 沂源县| 定州市| 黄梅县| 镇巴县| 南宫市| 田林县| 科技| 营口市| 上林县| 镇康县| 巨鹿县| 高雄县| 建水县| 十堰市| 来安县| 扬中市| 城口县| 巨野县| 石台县| 特克斯县| 开江县| 兴和县| 彰化市| 南陵县|