java LRU算法介紹與用法示例
本文實例講述了java LRU算法介紹與用法。分享給大家供大家參考,具體如下:
1.前言
在用戶使用聯(lián)網(wǎng)的軟件的時候,總會從網(wǎng)絡(luò)上獲取數(shù)據(jù),當在一段時間內(nèi)要多次使用同一個數(shù)據(jù)的時候,用戶不可能每次用的時候都去聯(lián)網(wǎng)進行請求,既浪費時間又浪費網(wǎng)絡(luò)
這時就可以將用戶請求過的數(shù)據(jù)進行保存,但不是任意數(shù)據(jù)都進行保存,這樣會造成內(nèi)存浪費的。LRU算法的思想就可以運用了。
2.LRU簡介
LRU是Least Recently Used 近期最少使用算法,它就可以將長時間沒有被利用的數(shù)據(jù)進行刪除。
LRU在人們一些情感中也體現(xiàn)的得很好的。當你和一群朋友接觸的時候,經(jīng)常聯(lián)系的人關(guān)系是很好的,若很久沒有聯(lián)系到最后估計都不會再有聯(lián)系了,也就會是失去這位朋友了。
3.下面通過代碼展現(xiàn)LRU算法:
最簡單的一種方法是利用LinkHashMap,因為它本身就有一個方法就是在所設(shè)置的緩存范圍內(nèi),去除掉額外的舊數(shù)據(jù)
public class LRUByHashMap<K, V> {
/*
* 通過LinkHashMap簡單實現(xiàn)LRU算法
*/
/**
* 緩存大小
*/
private int cacheSize;
/**
* 當前緩存數(shù)目
*/
private int currentSize;
private LinkedHashMap<K, V> maps;
public LRUByHashMap(int cacheSize1) {
this.cacheSize = cacheSize1;
maps = new LinkedHashMap<K, V>() {
/**
*
*/
private static final long serialVersionUID = 1;
// 這里移除舊的緩存數(shù)據(jù)
@Override
protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
// 當超過緩存數(shù)量的時候就將舊的數(shù)據(jù)移除
return getCurrentSize() > LRUByHashMap.this.cacheSize;
}
};
}
public synchronized int getCurrentSize() {
return maps.size();
}
public synchronized void put(K k, V v) {
if (k == null) {
throw new Error("存入的鍵值不能為空");
}
maps.put(k, v);
}
public synchronized void remove(K k) {
if (k == null) {
throw new Error("移除的鍵值不能為空");
}
maps.remove(k);
}
public synchronized void clear() {
maps = null;
}
// 獲取集合
public synchronized Collection<V> getCollection() {
if (maps != null) {
return maps.values();
} else {
return null;
}
}
public static void main(String[] args) {
// 測試
LRUByHashMap<Integer, String> maps = new LRUByHashMap<Integer, String>(
3);
maps.put(1, "1");
maps.put(2, "2");
maps.put(3, "3");
maps.put(4, "4");
maps.put(5, "5");
maps.put(6, "6");
Collection<String> col = maps.getCollection();
System.out.println("存入緩存中的數(shù)據(jù)是--->>" + col.toString());
}
}
運行后的效果:

代碼明明是put了6個Entry但最后只顯示了三個,之間的三個是舊的所以直接被咔嚓掉了
第二種方法是利用雙向鏈表 + HashTable
雙向鏈表的作用是用來記錄位置的,而HashTable作為容器來存儲數(shù)據(jù)的
為什么用HashTable不用HashMap呢?
1.HashTable的鍵和值都不能為null;
2.上面用LinkHashMap實現(xiàn)的LRU,有用到 synchronized , 讓線程同步去處理,這樣就避免在多線程處理統(tǒng)一數(shù)據(jù)時造成問題
而HashTable自帶同步機制的,所以多線程就能對HashTable進行正確的處理了。
Cache的所都用有位置雙連表連接起來,當一個位置被命中之后,就將通過調(diào)整鏈表的指向,將該位置調(diào)整到鏈表頭的位置,新加入的Cache直接加到鏈表 頭中。這樣,在多次進行Cache操作后,
最近被命中的,就會被向鏈表頭方向移動,而沒有命中的,而想鏈表后面移動,鏈表尾則表示最近最少使用的 Cache。當需要替換內(nèi)容時候,鏈表的最后位置就是最少被命中的位置,我們只需要淘
汰鏈表最后的部分即可
public class LRUCache {
private int cacheSize;
private Hashtable<Object, Entry> nodes;//緩存容器
private int currentSize;
private Entry first;//鏈表頭
private Entry last;//鏈表尾
public LRUCache(int i) {
currentSize = 0;
cacheSize = i;
nodes = new Hashtable<Object, Entry>(i);//緩存容器
}
/**
* 獲取緩存中對象,并把它放在最前面
*/
public Entry get(Object key) {
Entry node = nodes.get(key);
if (node != null) {
moveToHead(node);
return node;
} else {
return null;
}
}
/**
* 添加 entry到hashtable, 并把entry
*/
public void put(Object key, Object value) {
//先查看hashtable是否存在該entry, 如果存在,則只更新其value
Entry node = nodes.get(key);
if (node == null) {
//緩存容器是否已經(jīng)超過大小.
if (currentSize >= cacheSize) {
nodes.remove(last.key);
removeLast();
} else {
currentSize++;
}
node = new Entry();
}
node.value = value;
//將最新使用的節(jié)點放到鏈表頭,表示最新使用的.
node.key = key
moveToHead(node);
nodes.put(key, node);
}
/**
* 將entry刪除, 注意:刪除操作只有在cache滿了才會被執(zhí)行
*/
public void remove(Object key) {
Entry node = nodes.get(key);
//在鏈表中刪除
if (node != null) {
if (node.prev != null) {
node.prev.next = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
}
if (last == node)
last = node.prev;
if (first == node)
first = node.next;
}
//在hashtable中刪除
nodes.remove(key);
}
/**
* 刪除鏈表尾部節(jié)點,即使用最后 使用的entry
*/
private void removeLast() {
//鏈表尾不為空,則將鏈表尾指向null. 刪除連表尾(刪除最少使用的緩存對象)
if (last != null) {
if (last.prev != null)
last.prev.next = null;
else
first = null;
last = last.prev;
}
}
/**
* 移動到鏈表頭,表示這個節(jié)點是最新使用過的
*/
private void moveToHead(Entry node) {
if (node == first)
return;
if (node.prev != null)
node.prev.next = node.next;
if (node.next != null)
node.next.prev = node.prev;
if (last == node)
last = node.prev;
if (first != null) {
node.next = first;
first.prev = node;
}
first = node;
node.prev = null;
if (last == null)
last = first;
}
/*
* 清空緩存
*/
public void clear() {
first = null;
last = null;
currentSize = 0;
}
}
class Entry {
Entry prev;//前一節(jié)點
Entry next;//后一節(jié)點
Object value;//值
Object key;//鍵
}
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Java調(diào)用構(gòu)造函數(shù)和方法及使用詳解
在Java編程中,構(gòu)造函數(shù)用于初始化新創(chuàng)建的對象,而方法則用于執(zhí)行對象的行為,構(gòu)造函數(shù)在使用new關(guān)鍵字創(chuàng)建類實例時自動調(diào)用,沒有返回類型,并且名稱與類名相同,本文通過示例詳細介紹了如何在Java中使用構(gòu)造函數(shù)和方法,感興趣的朋友一起看看吧2024-10-10
Caused?by:?java.lang.NumberFormatException:?For?input?s
這篇文章主要介紹了Caused?by:?java.lang.NumberFormatException:?For?input?string:?“port“,本文給大家分享完美解決方法,需要的朋友可以參考下2023-01-01
springboot配置項目啟動后自動打開瀏覽器訪問項目方式
這篇文章主要介紹了springboot配置項目啟動后自動打開瀏覽器訪問項目方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
SpringSecurity頁面授權(quán)與登錄驗證實現(xiàn)(內(nèi)存取值與數(shù)據(jù)庫取值)
Spring Security是一個能夠為基于Spring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架,本文主要介紹了SpringSecurity頁面授權(quán)與登錄驗證實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06
java使用socket實現(xiàn)一個多線程web服務(wù)器的方法
今天小編就為大家分享一篇java使用socket實現(xiàn)一個多線程web服務(wù)器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10

