国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Java手動(dòng)實(shí)現Redis的LRU緩存機制

發(fā)布時(shí)間:2021-07-05 18:40 來(lái)源:腳本之家 閱讀:0 作者:拉霍拉卡 欄目: 開(kāi)發(fā)技術(shù)

目錄

前言

最近在逛博客的時(shí)候看到了有關(guān)Redis方面的面試題,其中提到了Redis在內存達到最大限制的時(shí)候會(huì )使用LRU等淘汰機制,然后找了這方面的一些資料與大家分享一下。 LRU總體大概是這樣的,最近使用的放在前面,最近沒(méi)用的放在后面,如果來(lái)了一個(gè)新的數,此時(shí)內存滿(mǎn)了,就需要把舊的數淘汰,那為了方便移動(dòng)數據,肯定就得使用鏈表類(lèi)似的數據結構,再加上要判斷這條數據是不是最新的或者最舊的那么應該也要使用hashmap等key-value形式的數據結構。

第一種實(shí)現(使用LinkedHashMap)

public class LRUCache {

    int capacity;
    Map<Integer,Integer> map;

    public LRUCache(int capacity){
        this.capacity = capacity;
        map = new LinkedHashMap<>();
    }

    public int get(int key){
        //如果沒(méi)有找到
        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,刪除最久沒(méi)用的即第一個(gè),或者可以復寫(xiě)removeEldestEntry方法
        if (map.size() > capacity){
            map.remove(map.entrySet().iterator().next().getKey());
        }
    }

    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(10);
        for (int i = 0; i < 10; i++) {
            lruCache.map.put(i,i);
            System.out.println(lruCache.map.size());
        }
        System.out.println(lruCache.map);
        lruCache.put(10,200);
        System.out.println(lruCache.map);
    }

第二種實(shí)現(雙鏈表+hashmap)

public class LRUCache {

    private int capacity;
    private Map<Integer,ListNode>map;
    private ListNode head;
    private ListNode tail;

    public LRUCache2(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;
        }
    }

    //把節點(diǎn)移動(dòng)到尾巴
    private void moveToTail(ListNode node) {
        node.pre = tail.pre;
        tail.pre = node;
        node.pre.next = node;
        node.next = tail;
    }

    //定義雙向鏈表節點(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;
        }
    }
}

補充

像第一種方式,如果復寫(xiě)removeEldestEntry會(huì )更簡(jiǎn)單,這里簡(jiǎn)單的展示一下

public class LRUCache extends LinkedHashMap<Integer,Integer> {


    private int capacity;
    
    @Override
    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
        return size() > capacity;
    }
}

以上就是Java手動(dòng)實(shí)現Redis的LRU緩存機制的詳細內容,更多關(guān)于Java 實(shí)現Redis的LRU緩存機制的資料請關(guān)注腳本之家其它相關(guān)文章!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

在线观看国产网址你懂的| 免费不卡在线观看AV| 女人18毛片A级毛片免费视频| 天天干夜夜操| 国产精品无码专区在线观看| 亚洲精品成人无码中文毛片|