溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Redis中怎么實(shí)現(xiàn)LRU緩存機(jī)制

發(fā)布時(shí)間:2021-08-10 11:37:59 來源:億速云 閱讀:119 作者:Leah 欄目:編程語言

本篇文章為大家展示了Redis中怎么實(shí)現(xiàn)LRU緩存機(jī)制,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

第一種實(shí)現(xiàn)(使用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){//如果沒有找到if (!map.containsKey(key)){return -1;
        }//找到了就刷新數(shù)據(jù)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) {
        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);
    }

Redis中怎么實(shí)現(xiàn)LRU緩存機(jī)制

第二種實(shí)現(xiàn)(雙鏈表+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;
        }
    }

    //把節(jié)點(diǎn)移動(dòng)到尾巴
    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;
        }
    }
}

像第一種方式,如果復(fù)寫removeEldestEntry會(huì)更簡單,這里簡單的展示一下

public class LRUCache extends LinkedHashMap<Integer,Integer> {private int capacity;    @Overrideprotected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {return size() > capacity;
    }
}

上述內(nèi)容就是Redis中怎么實(shí)現(xiàn)LRU緩存機(jī)制,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI