日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

LRU缓存简单实现

發(fā)布時(shí)間:2023/11/30 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LRU缓存简单实现 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

緩存接口定義

/*** 緩存接口* * @author zhi**/ public interface ICache<K, V> {/*** 添加緩存數(shù)據(jù)* * @param key* @param value*/void put(K key, V value);/*** 獲取緩存數(shù)據(jù)* * @param key* @return*/V get(K key);/*** 刪除緩存數(shù)據(jù)* * @param key* @return*/V remove(K key);/*** 清除緩存*/void clear(); }

利用LinkedHashMap實(shí)現(xiàn)

import java.util.LinkedHashMap; import java.util.Map;public class LRUCache<K, V> implements ICache<K, V> {private Map<K, V> map = null;@SuppressWarnings("serial")public LRUCache(final int maxCapacity) {map = new LinkedHashMap<K, V>(maxCapacity, 0.75f, true) {@Overrideprotected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) {return size() > maxCapacity;}};}@Overridepublic V get(K key) {return map.get(key);}@Overridepublic synchronized void put(K key, V value) {map.put(key, value);}@Overridepublic V remove(K key) {return map.remove(key);}@Overridepublic void clear() {map.clear();} }

?利用雙鏈表實(shí)現(xiàn)

import java.util.HashMap;public class LRUCache<K, V> implements ICache<K, V> {private final int maxCapacity;private CacheItem first;private CacheItem last;private HashMap<K, CacheItem> cache;public LRUCache(final int maxCapacity) {this.maxCapacity = maxCapacity;cache = new HashMap<K, CacheItem>(maxCapacity);}@Overridepublic V get(K key) {CacheItem item = cache.get(key);if (item == null) {return null;}moveToFirst(item);return item.value;}@Overridepublic synchronized void put(K key, V value) {CacheItem item = cache.get(key);if (item != null) {item.value = value;moveToFirst(item);} else {if (cache.size() == maxCapacity) {cache.remove(last.key);last = last.pre;if (last == null) {first = null;} else {last.next = null;}}item = new CacheItem(key, value);cache.put(key, item);if (first != null) {item.next = first;first.pre = item;}first = item;if (last == null) {last = item;}}}@Overridepublic V remove(K key) {if (cache.containsKey(key)) {CacheItem item = cache.get(key);if (item.pre != null) {item.pre.next = item.next;}if (item.next != null) {item.next.pre = item.pre;}if (item == first) {first = item.next;}if (item == last) {last = item.pre;}return cache.remove(key).value;} else {return null;}}private void moveToFirst(CacheItem item) {if (item == first) {return;}if (item.next == null) {last = item.pre;}item.pre.next = item.next;item.next = first;first = item;first.pre = null;}@Overridepublic void clear() {first = last = null;cache.clear();}class CacheItem {CacheItem pre;CacheItem next;K key;V value;public CacheItem(K key, V value) {this.key = key;this.value = value;}} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/zhi-leaf/p/10825553.html

總結(jié)

以上是生活随笔為你收集整理的LRU缓存简单实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。