LruCache
LruCache是Android3.1提供的緩存類,并且在v4包提供了該類。LruCache是一個(gè)泛型類,它內(nèi)部提供了LinkedHashMap作為存儲(chǔ)工具,被緩存的對(duì)象會(huì)添加到LinkedHashMap中。而LinkedHashMap持有對(duì)象是以強(qiáng)引用的方式,所以不會(huì)被系統(tǒng)回收掉。
源碼解析
- 構(gòu)造函數(shù)
public LruCache(int maxSize) {
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize <= 0");
}
this.maxSize = maxSize;
this.map = new LinkedHashMap<K, V>(0, 0.75f, true);
}
創(chuàng)建LruCache對(duì)象時(shí),它內(nèi)部做的工作如下:
- 記錄下制定的最大容量maxSize。
- 創(chuàng)建一個(gè)初始容量為0的LinkedHashMap,用來(lái)保存對(duì)象
所以,在創(chuàng)建LruCache對(duì)象時(shí),我們需要指定一個(gè)最大容量值maxSize,它可以是具體數(shù)值,也可以是內(nèi)存空間,與它對(duì)應(yīng)的是sizeOf方法:
protected int sizeOf(K key, V value) {
return 1;
}
如果我們想以數(shù)量的方式來(lái)控制緩存,可以看到,默認(rèn)的sizeOf方法返回值為1,也就是說(shuō)每次添加一條數(shù)據(jù),它所占用的空間為1,直到添加的內(nèi)容數(shù)量達(dá)到maxSize,這時(shí)候再添加數(shù)據(jù),系統(tǒng)就會(huì)挑選出使用頻率最低的值,將他們移除。
如果以內(nèi)存空間作為緩存限制,需要重寫(xiě)size,返回對(duì)象所占用的空間大小:
int maxMemory = (int) (Runtime.getRuntime().maxMemory())/1024;//獲取可用內(nèi)存,轉(zhuǎn)換為單位KB
int maxCache = maxMemory/10;//緩存大小為可用內(nèi)存的1/10
LruCache mMemoryCache = new LruCache<String,Bitmap>(maxCache){
@Override//重寫(xiě)sizeOf方法,返回每條數(shù)據(jù)占用的內(nèi)存
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes()*value.getHeight() / 1024;
//return value.getByteCount() / 1024;
}
};
- 讀取內(nèi)容
讀取內(nèi)容對(duì)應(yīng)的是get方法,源碼如下:
public final V get(K key) {
if (key == null) { //如果key為null,直接拋出異常
throw new NullPointerException("key == null");
}
V mapValue;
synchronized (this) {
mapValue = map.get(key); //讀取key對(duì)應(yīng)的值
if (mapValue != null) {
hitCount++;
return mapValue; //如果獲取到內(nèi)容,返回
}
missCount++; //如果未讀到內(nèi)容,missCount計(jì)數(shù)增加,繼續(xù)向下執(zhí)行
}
/*嘗試創(chuàng)建value,可能會(huì)消耗一定的時(shí)間,當(dāng)創(chuàng)建完成時(shí),map可能已經(jīng)發(fā)生變化
如果此時(shí)發(fā)現(xiàn)創(chuàng)建時(shí)使用的key已經(jīng)存在,導(dǎo)致沖突,丟棄掉create創(chuàng)建的值
* Attempt to create a value. This may take a long time, and the map
* may be different when create() returns. If a conflicting value was
* added to the map while create() was working, we leave that value in
* the map and release the created value.
*/
//如果未獲取到value,調(diào)用create方法,嘗試創(chuàng)建對(duì)象
//create方法默認(rèn)返回null,可以覆寫(xiě)該方法,決定獲取不到值時(shí)的添加方法
V createdValue = create(key);
if (createdValue == null) {
return null;
}
//以下部分為創(chuàng)建之后對(duì)map的修改動(dòng)作,包括存放對(duì)象,調(diào)整size等。
synchronized (this) {
createCount++;
mapValue = map.put(key, createdValue);
if (mapValue != null) {
// There was a conflict so undo that last put
map.put(key, mapValue);
} else {
size += safeSizeOf(key, createdValue);
}
}
if (mapValue != null) {
entryRemoved(false, key, createdValue, mapValue);
return mapValue;
} else {
trimToSize(maxSize);
return createdValue;
}
}
get方法嘗試根據(jù)傳入的key來(lái)讀取內(nèi)容,如果讀取不到,可以選擇是否創(chuàng)建一個(gè)對(duì)象,如果選擇創(chuàng)建對(duì)象,需要我們覆寫(xiě)create方法來(lái)返回要?jiǎng)?chuàng)建的對(duì)象,使用也很簡(jiǎn)單
- 存儲(chǔ)內(nèi)容
存儲(chǔ)內(nèi)容使用的是put方法:
public final V put(K key, V value) {
if (key == null || value == null) {
throw new NullPointerException("key == null || value == null");
}
V previous;
synchronized (this) {
putCount++;
size += safeSizeOf(key, value);
previous = map.put(key, value);
if (previous != null) {
size -= safeSizeOf(key, previous);
}
}
if (previous != null) {
entryRemoved(false, key, previous, value);
}
trimToSize(maxSize);
return previous;
}
在存放內(nèi)容的時(shí)候,可以看到是怎樣控制緩存size的:存放內(nèi)容之后會(huì)將執(zhí)行size += safeSizeOf(key, value);,safeSizeOf的默認(rèn)實(shí)現(xiàn)就是sizeOf方法。 每存放一個(gè)對(duì)象,就會(huì)將size增加對(duì)應(yīng)的值。如果存放的key已經(jīng)存在數(shù)據(jù),那么size不變。
同時(shí)還提供了一個(gè)entryRemoved方法,該方法在數(shù)據(jù)被移除時(shí)(調(diào)用remove移除,新值覆蓋,超出緩存被刪除)調(diào)用,默認(rèn)實(shí)現(xiàn)為空。
在put的最后則是調(diào)用了trimToSize,這是控制緩存大小的方法,每當(dāng)有新的數(shù)據(jù)存入時(shí),該方法都會(huì)被調(diào)用。當(dāng)前size超出緩存最大值之后,會(huì)通過(guò)此方法刪除最近最少使用的數(shù)據(jù)。
除了正常的存儲(chǔ),讀取之外,LruCache還提供了一個(gè)一次性讀取全部緩存對(duì)象的方法:
public synchronized final Map<K, V> snapshot() {
return new LinkedHashMap<K, V>(map);
}