在開發中我們會經常碰到一些資源需要做緩存優化,例如Bitmap,Json等,那么今天我們來瞧瞧默默無聞的LruCache的實現原理
Ps:本文基于API25
簡介
當我們做數據緩存處理的時候緩存大小到達臨界值時我們會面臨2個選擇,一個是擴容,一個是清理緩存,而LruCache就是一種屬于選擇清理緩存的方式,清理最長時間未使用的數據。
分析
按照慣例,我們從入口開始,直接看v4包下的好了,和普通的LruCache幾乎沒有代碼出入,先來看看構造方法
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);
}
在構造方法中需要傳入一個閾值,也就是緩存大小的上限,內部有一個LinkedHashMap
作為強引用來保存,我們來看看LinkedHashMap
構造器里面發生了什么
public class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>
{//省略其他代碼
public LinkedHashMap(int initialCapacity,
float loadFactor,
boolean accessOrder) {
super(initialCapacity, loadFactor);
this.accessOrder = accessOrder;
}
}
LinkedHashMap
繼承于HashMap
,對HashMap還不了解的同學要趕緊補補了。
在開發中我們遍歷HashMap
的Entry
會發現它不是按插入順序排序的,而LinkedHashMap
的機制會將每一個數據節點前后鏈起來,是一個雙向循環鏈表的數據結構。
在使用LinkedHashMap
我們用無參構造的時候,是按順序排列的,取個例子
LinkedHashMap<String,String> map=new LinkedHashMap<>();
map.put("1","1");
map.put("2","2");
map.put("3","3");
map.put("1","4");
Iterator<Map.Entry<String, String>> i = map.entrySet().iterator();
while (i.hasNext()) {
Map.Entry<String, String> e = i.next();
Log.e("Entry", e.getKey() + " " + e.getValue());
}
這種時候日志會輸出
Entry: 1 4
Entry: 2 2
Entry: 3 3
因為map.put("1","4")
把原來的值覆蓋了,不影響鏈表排序,
那么我們來看這個accessOrder
開關,默認是false,翻譯出來是存取順序,開啟了會發生什么
Entry: 2 2
Entry: 3 3
Entry: 1 4
順序發生了改變!我們來看看這個accessOrder
影響了什么邏輯
private static class LinkedHashMapEntry<K,V> extends HashMapEntry<K,V> {
//省略
private void remove() {
//原來前后的數據節點鏈在一起
before.after = after;
after.before = before;
}
private void addBefore(LinkedHashMapEntry<K,V> existingEntry) {
after = existingEntry;
before = existingEntry.before;
before.after = this;
after.before = this;
}
void recordAccess(HashMap<K,V> m) {
LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
if (lm.accessOrder) {
lm.modCount++;
//移出當前的Entry結構
remove();
//移動到隊列尾部
addBefore(lm.header);
}
}
//省略
}
public V get(Object key) {
LinkedHashMapEntry<K,V> e = (LinkedHashMapEntry<K,V>)getEntry(key);
if (e == null)
return null;
//獲取數據后刷新位置
e.recordAccess(this);
return e.value;
}
LinkedHashMap
額外采用了鏈表的設計,這么一看,完全符合LruCache近期最少使用的策略,我們來完整的看一下LruCache的成員變量:
public class LruCache<K, V> {
//強引用保存
private final LinkedHashMap<K, V> map;
/** Size of this cache in units. Not necessarily the number of elements. */
private int size;
//緩存上限
private int maxSize;
//put的次數
private int putCount;
//create的次數
private int createCount;
//移除的次數
private int evictionCount;
//命中緩存的次數
private int hitCount;
//未命中緩存的次數
private int missCount;
}
可以發現LruCache的成員變量異常簡單,出去一些計數的變量外,就一個LinkedHashMap
來保存我們的緩存數據,
我們先來看看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) {
//計數+1
putCount++;
//計算放入的大小
size += safeSizeOf(key, value);
previous = map.put(key, value);
if (previous != null) {
//此次put是覆蓋數據,減去計算的大小
size -= safeSizeOf(key, previous);
}
}
if (previous != null) {
//空方法,用于通知
entryRemoved(false, key, previous, value);
}
//修改尺寸
trimToSize(maxSize);
return previous;
}
private int safeSizeOf(K key, V value) {
int result = sizeOf(key, value);
if (result < 0) {
throw new IllegalStateException("Negative size: " + key + "=" + value);
}
return result;
}
protected int sizeOf(K key, V value) {
return 1;
}
在put方法內部對放入的value進行了大小計算,也就是說我們在使用LruCache需要重寫sizeOf
方法,要不然LruCache無法對緩存空間進行計算。
接著當我們put
時如果覆蓋了新數據時,會回調entryRemoved
方法,然后LruCache會調用trimToSize
對當前的map空間進行計算,代碼如下:
public void trimToSize(int maxSize) {
while (true) {
K key;
V value;
synchronized (this) {
if (size < 0 || (map.isEmpty() && size != 0)) {
throw new IllegalStateException(getClass().getName()
+ ".sizeOf() is reporting inconsistent results!");
}
if (size <= maxSize || map.isEmpty()) {
//不到上限時跳出死循環
break;
}
Map.Entry<K, V> toEvict = map.entrySet().iterator().next();
key = toEvict.getKey();
value = toEvict.getValue();
//移除最開始放入的
map.remove(key);
size -= safeSizeOf(key, value);
//移除數+1
evictionCount++;
}
//空方法,回調
entryRemoved(true, key, value, null);
}
}
在trimToSize
方法中是一個死循環,只要當前map的空間大于上限,就將其移除隊列,并且回調entryRemoved
,那么我們來看看
protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {}
參數分別代表
- 是否因為大小被驅逐,555~~
- key
- 老數據
- 新數據,可能為null
這里trimToSize
方法是public,也就是說比如在內存緊張的時候可以手動清理一部分緩存。接下來我們來看看get
方法:
public final V get(K key) {
if (key == null) {
throw new NullPointerException("key == null");
}
V mapValue;
synchronized (this) {
//獲取數據
mapValue = map.get(key);
if (mapValue != null) {
//命中+1,并返回
hitCount++;
return mapValue;
}
missCount++;
}
//空方法,默認返回null
V createdValue = create(key);
if (createdValue == null) {
return null;
}
synchronized (this) {
createCount++;
//將創建出來的數據放入
mapValue = map.put(key, createdValue);
if (mapValue != null) {
//對應的key原來有value的情況下,那就再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;
}
}
其中有一個空方法create
,有需求可以重寫,就是在根據key
去尋找value
時,如果找不到,可以選擇創建一個value
并放入到緩存隊列中。
總結
LruCache源碼異常的精簡,核心原理是通過LinkedHashMap
雙向循環鏈表,每次訪問過的數據會被移動到隊列末尾,在使用過程中我們需要重寫sizeOf
方法來幫助LruCache計算緩存大小,每當緩存數據發生覆蓋或者清理時會回調entryRemoved
方法,并且LruCache是線程安全的,核心操作都上了同步鎖。
Ps:我們可以手動調用trimToSize
清理一批數據,也可以調用resize
方法,重新賦值緩存大小的上限并計算當前空間是否需要清理,snapshot
來獲取緩存map的切片,注意是淺拷貝。
文章如有錯誤,敬請指正!
本文的姊妹篇:DiskLruCache源碼分析
“ 神愛世人,甚至將他的獨生子賜給他們,叫一切信他的,不至滅亡,反得永生。 (約翰福音 3:16 和合本)