LRU 實現緩存

LRU:Least Recently used 最近最少使用

?

1.使用LinkedHashMap實現 inheritance實現方式 繼承map類 可以使用Collections.synchronizedMap方式實現線程安全的操作

public class LruCache<K,V> extends LinkedHashMap<K,V> {private final int MAX_CACHE_SIZE;public LruCache(int cacheSize) {super((int)Math.ceil(cacheSize/0.75)+1,0.75f,true );MAX_CACHE_SIZE = cacheSize;}@Overrideprotected boolean removeEldestEntry(Map.Entry eldest){return size()> MAX_CACHE_SIZE;}@Overridepublic String toString(){StringBuilder sb= new StringBuilder();for(Map.Entry<K,V> entry : entrySet()){sb.append(String.format("%s:%s",entry.getKey(),entry.getValue()));}return sb.toString();}
}

2、LinkedHashMap 使用delegation方式實現

沒有map接口?

public class LruByDelegation<K,V> {private final int MAX_CACHE_SIZE;private final float DEFAULT_LOAD_FACTOR = 0.75f;LinkedHashMap<K,V> map;public LruByDelegation(int cacheSize) {this.MAX_CACHE_SIZE = cacheSize;int capacity = (int) (Math.ceil(MAX_CACHE_SIZE/DEFAULT_LOAD_FACTOR)+1);map= new LinkedHashMap(capacity,DEFAULT_LOAD_FACTOR,true){@Overrideprotected boolean removeEldestEntry(Map.Entry eldest){return size()>MAX_CACHE_SIZE;}};}public synchronized void put(K key,V value){map.put(key,value);}public synchronized V get(K key){return map.get(key);}public synchronized void remove(K key){map.remove(key);}public synchronized Set<Map.Entry<K,V>> getAll(){return map.entrySet();}public synchronized int size(){return map.size();}public synchronized void clear(){map.clear();}@Overridepublic String toString(){StringBuilder sb= new StringBuilder();for(Map.Entry entry : map.entrySet()){sb.append(String.format("%s:%s",entry.getKey(),entry.getValue()));}return sb.toString();}}

2 Cache鏈表+HashMap實現? ?Entry自己定義? 總結一下就是各種pre 和 next指針的變換

public class LruCache01<K,V> {private final int MAX_CACHE_SIZE;private Entry first;private Entry last;private HashMap<K, Entry<K,V>> hashMap;public LruCache01(int MAX_CACHE_SIZE) {this.MAX_CACHE_SIZE = MAX_CACHE_SIZE;hashMap=new HashMap<>();}public void put(K key, V value){Entry entry = getEntry(key);if(entry==null){if(hashMap.size()>=MAX_CACHE_SIZE){hashMap.remove(last.key);//removeLast
                removeLast();}entry=new Entry();entry.key=key;}entry.value=value;moveToFirst(entry);hashMap.put(key,entry);}public V get(K  key){Entry entry  = getEntry(key);if(entry==null)return null;moveToFirst(entry);return (V) entry.value;}public void remove(K key){Entry entry = getEntry(key);if(entry!=null){if(entry.pre!=null)entry.pre.next=entry.next;if(entry.next!=null)entry.next.pre=entry.pre;if(entry==first)first=entry.next;if(entry==last)last=entry.pre;}hashMap.remove(key);}public void moveToFirst(Entry entry){if(entry==first)return;if(entry.pre!=null)entry.pre.next=entry.next;if(entry.next!=null)entry.next.pre=entry.pre;if(entry==last)last=last.pre;if(first==null || last==null){first=last=entry;return;}entry.next=first;first.pre=entry;first=entry;entry.pre=null;}public void removeLast(){if(last!=null){last=last.pre;if(last==null)first=null;elselast.next=null;}}public Entry<K,V> getEntry(K key){return hashMap.get(key);}@Overridepublic String toString(){StringBuilder sb= new StringBuilder();Entry entry = first;while(entry!=null){sb.append(String.format("%s:%s",entry.key,entry.value));entry=entry.next;}return sb.toString();}
}
class Entry<K,V>{public Entry pre;public Entry next;public K key;public V value;
}

LinkedHashMap的FIFO實現

只需要重新removeEldestEntry方法可以實現FIFO緩存

final int cacheSize=5;
LinkedHashMap<Integer,String> lru = new LinkedHashMap<Integer,String>(){@Override
protected boolean removeEldestEntry(Map.Entry<Integer,String> eldest){
return size()>cacheSize;}  
}

?

轉載于:https://www.cnblogs.com/zhy-study/p/9498560.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/451573.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/451573.shtml
英文地址,請注明出處:http://en.pswp.cn/news/451573.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用vsftp作為集群的yum倉庫

地址規劃&#xff1a;vsftp服務器的地址為172.16.1.61使用的環境&#xff1a;[rootnfs01 scripts]# uname -a Linux nfs01 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux首先在yum服務器上掛載本地光盤mkdir /media/cdrom ;mount…

純做技術是自娛自樂 拋開技術做技術才是出路

短短一生不過數十載&#xff0c;對于很多人而言&#xff0c;作IT、作技術只是生命中的某一段&#xff0c;并非所有。而無論是換工作還是換行業&#xff0c;只是一種形式而已&#xff0c;最終我們追求的是成功、是榮譽、是收獲。于是在年輕的這幾年里&#xff0c;作為技術人員理…

TOAD連接Oracle數據庫失敗:OCI_INVALID_HANDLE解決

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. toad 連接Oracle數據庫連接失敗如圖&#xff1a; 2. 導致這個情況的前因&#xff1a;toad運行情況下&#xff0c;突然斷電。 3. 解決…

多線程三大特性:原子性、有序性、可見性

參考文獻&#xff1a;三大性質總結&#xff1a;原子性&#xff0c;有序性&#xff0c;可見性 感謝作者分享&#xff01;

git checkout 和 git reset

git checkout 主要有三個作用&#xff1a; 第一個就是切換分支。例如你從遠程倉庫clone下來所有的源代碼&#xff0c;你git branch一下會看到你通常是在master&#xff0c;如果你想切換到某一個分支上呢&#xff1f;git checkout <branchname>第二個就是放棄對某個文件的…

python-訪問者模式

源碼地址:https://github.com/weilanhanf/PythonDesignPatterns 說明&#xff1a; 訪問者模式的基本想法是&#xff0c;軟件系統中擁有一個由許多對象構成的、比較穩定的對象結構&#xff0c;這些對象的類都擁有一個 accept 方法用來接受訪問者對象的訪問。訪問者是一個接口&am…

面試題:Fibonacci數列

題目描述&#xff1a;大家都知道斐波那契數列&#xff0c;現在要求輸入一個整數n&#xff0c;請你輸出斐波那契數列的第n項&#xff08;從0開始&#xff0c;第0項為0&#xff09;。 方法1&#xff1a;遞歸 public class Solution {public int Fibonacci(int n) {if (n 0){retu…

“行到水窮處,坐看云起時.“

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 自由自在&#xff0c;隨意而行&#xff0c; 只沿著流水向上&#xff0c;不知不覺的就走到了泉眼盡頭&#xff0c; 無路可走的時候 &…

git commit -m和git commit -am

字面解釋的話&#xff0c;git commit -m用于提交暫存區的文件&#xff1b;git commit -am用于提交跟蹤過的文件 要理解它們的區別&#xff0c;首先要明白git的文件狀態變化周期&#xff0c;如下圖所示 工作目錄下面的所有文件都不外乎這兩種狀態&#xff1a;已跟蹤或未跟蹤。已…

磁盤結構簡介

這里講的主要是網上所謂的老式磁盤&#xff0c;它是由一個個盤片組成的&#xff0c;我們先從個盤片結構講起。如圖1所示&#xff0c;圖中的一圈圈灰色同心圓為一條條磁道&#xff0c;從圓心向外畫直線&#xff0c;可以將磁道劃分為若干個弧段&#xff0c;每個磁道上一個弧段被稱…

java中的對象監視器

參考文章&#xff1a;監視器–JAVA同步基本概念 感謝作者分享&#xff01;

Yii1.1 CGridView 簡單使用

Yii1.1 CGridView 簡單使用 配置model文件&#xff0c;返回CActiveDataProvider對象。public function search() {$criterianew CDbCriteria;$criteria->compare(title,$this->title,true);$criteria->compare(type,$this->type);$criteria->compare(addr,$this…

3個著名加密算法(MD5、RSA、DES)的解析

MD5的全稱是Message-Digest Algorithm 5&#xff0c;在90年代初由MIT的計算機科學實驗室和RSA Data Security Inc發明&#xff0c;經MD2、MD3和MD4發展而來。 MD5將任意長度的“字節串”變換成一個128bit的大整數&#xff0c;并且它是一個不可逆的字符串變換算法&#x…

想念我的大大的石

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 // ------- 甘愿用我的一生去追尋 ... 想念我的大石頭&#xff1a; 想念會默默陪著我&#xff0c;一直從烈日咫尺坐到黃昏浸透蔓蔓云層…

Java 中的悲觀鎖、樂觀鎖、自旋鎖、適應性自旋鎖、偏向鎖、輕量級鎖、重量級鎖、公平鎖、非公平鎖、可重入鎖、共享鎖等

參考文獻&#xff1a; 不可不說的Java“鎖”事 java并發進階 感謝美團技術團隊&#xff01; 感謝JavaGuide&#xff01;

Git 的origin和master解析

首先要明確一點&#xff0c;對git的操作是圍繞3個大的步驟來展開的&#xff08;其實幾乎所有的SCM都是這樣&#xff09; 1. 從git取數據&#xff08;git clone&#xff09; 2. 改動代碼 3. 將改動傳回git&#xff08;git push&#xff09; 這3個步驟又涉及到兩個re…

end to end testing

概念 https://www.softwaretestinghelp.com/what-is-end-to-end-testing/ What is “End to End Testing”? Term “End to End testing” is defined as a testing method which determines whether the performance of an application is as per the requirement or not. It…

windows下安裝mysql 開機啟動

1 下載地址 http://dev.mysql.com/downloads/installer/ 2 下載版本 mysql community server 5.7.x 這個版本是一個傻瓜版本&#xff0c;設置root密碼之后就可以啟動服務了&#xff0c;不用自己配置&#xff0c;還有workbench可用。轉載于:https://www.cnblogs.com/hustdc/p/91…

Linux目錄架構詳解

Linux和Windows操作系統的顯著區別之一就是目錄架構的不同。Linux操作系統的目錄架構遵循文件系統層級結構標準。不知你是否使用ls命令瀏覽過Linux的根目錄“/”&#xff0c;親愛的讀者&#xff0c;您都了解這些目錄的含義嗎&#xff1f; ls -l / 遍歷文件系統&#xff08;點擊…

越陽光明媚....

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 窗外陽光明媚&#xff0c;而心卻如此哀傷... 很喜歡陽光明媚&#xff0c;很喜歡春暖花開&#xff0c; 窗外有幾片莊稼地&#xff1a;滿…