第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Java中易于使用的LRU緩存

在Java中易于使用的LRU緩存

一只萌萌小番薯 2019-10-16 14:13:44
我知道實現(xiàn)起來很簡單,但是我想重用已經(jīng)存在的東西。我要解決的問題是我為不同的頁面,角色加載了配置(從XML,所以我想緩存它們),因此輸入的組合可以增長很多(但99%的增長)。為了處理這個1%,我想在緩存中設(shè)置一些最大項目...直到我在apache commons中找到了org.apache.commons.collections.map.LRUMap,它看起來還不錯,但還想檢查其他內(nèi)容。有什么建議嗎?
查看完整描述

3 回答

?
繁華開滿天機

TA貢獻1816條經(jīng)驗 獲得超4個贊

您可以使用LinkedHashMap(Java 1.4+):


// Create cache

final int MAX_ENTRIES = 100;

Map cache = new LinkedHashMap(MAX_ENTRIES+1, .75F, true) {

    // This method is called just after a new entry has been added

    public boolean removeEldestEntry(Map.Entry eldest) {

        return size() > MAX_ENTRIES;

    }

};


// Add to cache

Object key = "key";

cache.put(key, object);


// Get object

Object o = cache.get(key);

if (o == null && !cache.containsKey(key)) {

    // Object not in cache. If null is not a possible value in the cache,

    // the call to cache.contains(key) is not needed

}


// If the cache is to be used by multiple threads,

// the cache must be wrapped with code to synchronize the methods

cache = (Map)Collections.synchronizedMap(cache);


查看完整回答
反對 回復(fù) 2019-10-16
  • 3 回答
  • 0 關(guān)注
  • 342 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號