淺談java如何實(shí)現(xiàn)Redis的LRU緩存機(jī)制
最近使用的放在前面,最近沒用的放在后面,如果來了一個(gè)新的數(shù),此時(shí)內(nèi)存滿了,就需要把舊的數(shù)淘汰,那為了方便移動數(shù)據(jù),肯定就得使用鏈表類似的數(shù)據(jù)結(jié)構(gòu),再加上要判斷這條數(shù)據(jù)是不是最新的或者最舊的那么應(yīng)該也要使用hashmap等key-value形式的數(shù)據(jù)結(jié)構(gòu)。
使用LinkedHashMap實(shí)現(xiàn)package thread;import java.util.LinkedHashMap;import java.util.Map; public class LRUCacheTest { int capacity; Map<Integer,Integer> map; public LRUCacheTest(int capacity){this.capacity = capacity;map = new LinkedHashMap<>(); } public int get(int key){//沒有找到 if(!map.containsKey(key)){ return -1; } Integer value = map.remove(key); map.put(key,value); return value; } public void put(int key,int value){if(map.containsKey(key)){ map.remove(key); map.put(key,value); return;}map.put(key,value);//超出capacity,刪除最久沒用的即第一個(gè),或者可以復(fù)寫removeEldestEntry方法if(map.size() > capacity){ map.remove(map.entrySet().iterator().next().getKey());} } public static void main(String[] args) {LRUCacheTest lruCache = new LRUCacheTest(10);for (int i = 0; i < 10; i++) { lruCache.map.put(i,i); System.out.print(lruCache.map.size()+'t');}System.out.println();System.out.println(lruCache.map);lruCache.put(10,200);System.out.println(lruCache.map);lruCache.put(11,100);System.out.println(lruCache.map);lruCache.get(2);System.out.println(lruCache.map); } }
結(jié)果來看是正確的,距離當(dāng)前時(shí)間最遠(yuǎn)的數(shù)據(jù)被淘汰
使用LinkedHashMap簡單方法實(shí)現(xiàn)LinkedHashMap是維護(hù)了雙向鏈表的HashMap,保持了插入元素的順序。
LinkedHashMap提供了一個(gè)鉤子方法,在新插入元素后可以決定是否刪除最老的元素。
復(fù)寫removeEldestEntry實(shí)現(xiàn)
package thread;import java.util.LinkedHashMap;import java.util.Map; public class LRUByLinkedHashMap extends LinkedHashMap<Integer,Integer> { /** * LRU中最大元素?cái)?shù)量 */ private int maxSize; public LRUByLinkedHashMap(int maxSize) {// 容量為最大值/0.75,即最大負(fù)載容量為maxSize// accessOrder=true 根據(jù)查詢排序,即最近被使用的放到后面super((int) Math.ceil(maxSize / 0.75) + 1, 0.75f, true);this.maxSize = maxSize; } /** * 此方法為鉤子方法,map插入元素時(shí)會調(diào)用此方法 * 此方法返回true則證明刪除最老的因子 * @param eldest * @return */ @Override protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {return size() > maxSize; } public static void main(String[] args) {LRUByLinkedHashMap hashMap = new LRUByLinkedHashMap(10);for (int i = 0; i < 10; i++) { hashMap.put(i,i); System.out.print(hashMap.size()+'t');}System.out.println();System.out.println(hashMap);hashMap.put(10,200);System.out.println(hashMap);hashMap.put(11,100);System.out.println(hashMap);hashMap.get(10);System.out.println(hashMap); } }
package thread;import java.util.HashMap;import java.util.Map; public class LRURedis { private int capacity; private Map<Integer,ListNode> map; private ListNode head; private ListNode tail; public LRURedis(int capacity){ this.capacity = capacity; map = new HashMap<>(); head = new ListNode(-1,-1); tail = new ListNode(-1,-1); head.next = tail; tail.pre = head; } public int get(int key){if(!map.containsKey(key)){ return -1;}ListNode node = map.get(key);node.pre.next = node.next;node.next.pre = node.pre;return node.val; } public void put(int key,int value){if (get(key)!=-1){ map.get(key).val = value; return;} ListNode node = new ListNode(key,value);map.put(key,node);moveToTail(node); if (map.size() > capacity){ map.remove(head.next.key); head.next = head.next.next; head.next.pre = head;} } //把節(jié)點(diǎn)移動到尾巴 private void moveToTail(ListNode node) {node.pre = tail.pre;tail.pre = node;node.pre.next = node;node.next = tail; } //定義雙向鏈表節(jié)點(diǎn) private class ListNode{int key;int val;ListNode pre;ListNode next; //初始化雙向鏈表public ListNode(int key,int val){ this.key = key; this.val = val; pre = null; next = null;} }}
到此這篇關(guān)于淺談java如何實(shí)現(xiàn)Redis的LRU緩存機(jī)制的文章就介紹到這了,更多相關(guān)java實(shí)現(xiàn)Redis的LRU緩存機(jī)制內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. html中的form不提交(排除)某些input 原創(chuàng)2. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. jsp文件下載功能實(shí)現(xiàn)代碼5. 開發(fā)效率翻倍的Web API使用技巧6. ASP常用日期格式化函數(shù) FormatDate()7. js select支持手動輸入功能實(shí)現(xiàn)代碼8. CSS3中Transition屬性詳解以及示例分享9. asp.net core項(xiàng)目授權(quán)流程詳解10. CSS3實(shí)現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效
