Java手動實現(xiàn)Redis的LRU緩存機制
前言
最近在逛博客的時候看到了有關(guān)Redis方面的面試題,其中提到了Redis在內(nèi)存達到最大限制的時候會使用LRU等淘汰機制,然后找了這方面的一些資料與大家分享一下。 LRU總體大概是這樣的,最近使用的放在前面,最近沒用的放在后面,如果來了一個新的數(shù),此時內(nèi)存滿了,就需要把舊的數(shù)淘汰,那為了方便移動數(shù)據(jù),肯定就得使用鏈表類似的數(shù)據(jù)結(jié)構(gòu),再加上要判斷這條數(shù)據(jù)是不是最新的或者最舊的那么應(yīng)該也要使用hashmap等key-value形式的數(shù)據(jù)結(jié)構(gòu)。
第一種實現(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,刪除最久沒用的即第一個,或者可以復(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);
}

第二種實現(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é)點移動到尾巴
private void moveToTail(ListNode node) {
node.pre = tail.pre;
tail.pre = node;
node.pre.next = node;
node.next = tail;
}
//定義雙向鏈表節(jié)點
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會更簡單,這里簡單的展示一下
public class LRUCache extends LinkedHashMap<Integer,Integer> {
private int capacity;
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}
}
以上就是Java手動實現(xiàn)Redis的LRU緩存機制的詳細內(nèi)容,更多關(guān)于Java 實現(xiàn)Redis的LRU緩存機制的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
jsp頁面中獲取servlet請求中的參數(shù)的辦法詳解
在JAVA WEB應(yīng)用中,如何獲取servlet請求中的參數(shù),本文講解了jsp頁面中獲取servlet請求中的參數(shù)的辦法2018-03-03
Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證問題
OpenCV是一個基于Apache2.0許可(開源)發(fā)行的跨平臺計算機視覺和機器學(xué)習(xí)軟件庫,可以運行在Linux、Windows、Android和Mac?OS操作系統(tǒng)上,這篇文章主要介紹了Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證,需要的朋友可以參考下2022-07-07
SpringBoot實現(xiàn)動態(tài)增刪啟停定時任務(wù)的方式
在spring?boot中,可以通過@EnableScheduling注解和@Scheduled注解實現(xiàn)定時任務(wù),也可以通過SchedulingConfigurer接口來實現(xiàn)定時任務(wù),但是這兩種方式不能動態(tài)添加、刪除、啟動、停止任務(wù),本文給大家介紹SpringBoot實現(xiàn)動態(tài)增刪啟停定時任務(wù)的方式,感興趣的朋友一起看看吧2024-03-03
Java中main函數(shù)的String[]?args用法舉例詳解
這篇文章主要給大家介紹了關(guān)于Java中main函數(shù)的String[]?args用法的相關(guān)資料,JAVA類中main函數(shù)的參數(shù)String[]?args指的是運行時給main函數(shù)傳遞的參數(shù),文中通過圖文以及代碼介紹的非常詳細,需要的朋友可以參考下2023-12-12
使用Spring Boot+MyBatis框架做查詢操作的示例代碼
這篇文章主要介紹了使用Spring Boot+MyBatis框架做查詢操作的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10

