Glide--LruCache原始碼分析
LruCache是一個記憶體快取工具類,先貼原始碼
public class LruCache<T, Y> {
private final LinkedHashMap<T, Y> cache = new LinkedHashMap<>(100, 0.75f, true);
private final int initialMaxSize;
private int maxSize;
private int currentSize = 0;
/**
* Constructor for LruCache.
*
* @param size The maximum size of the cache, the units must match the units used in {@link
* #getSize(Object)}.
*/
public LruCache(int size) {
this.initialMaxSize = size;
this.maxSize = size;
}
/**
* Sets a size multiplier that will be applied to the size provided in the constructor to put the
* new size of the cache. If the new size is less than the current size, entries will be evicted
* until the current size is less than or equal to the new size.
*
* @param multiplier The multiplier to apply.
*/
public synchronized void setSizeMultiplier(float multiplier) {
if (multiplier < 0) {
throw new IllegalArgumentException("Multiplier must be >= 0");
}
maxSize = Math.round(initialMaxSize * multiplier);
evict();
}
/**
* Returns the size of a given item, defaulting to one. The units must match those used in the
* size passed in to the constructor. Subclasses can override this method to return sizes in
* various units, usually bytes.
*
* @param item The item to get the size of.
*/
protected int getSize(Y item) {
return 1;
}
/**
* A callback called whenever an item is evicted from the cache. Subclasses can override.
*
* @param key The key of the evicted item.
* @param item The evicted item.
*/
protected void onItemEvicted(T key, Y item) {
// optional override
}
/**
* Returns the current maximum size of the cache in bytes.
*/
public synchronized int getMaxSize() {
return maxSize;
}
/**
* Returns the sum of the sizes of all items in the cache.
*/
public synchronized int getCurrentSize() {
return currentSize;
}
/**
* Returns true if there is a value for the given key in the cache.
*
* @param key The key to check.
*/
public synchronized boolean contains(T key) {
return cache.containsKey(key);
}
/**
* Returns the item in the cache for the given key or null if no such item exists.
*
* @param key The key to check.
*/
@Nullable
public synchronized Y get(T key) {
return cache.get(key);
}
/**
* Adds the given item to the cache with the given key and returns any previous entry for the
* given key that may have already been in the cache.
*
* <p> If the size of the item is larger than the total cache size, the item will not be added to
* the cache and instead {@link #onItemEvicted(Object, Object)} will be called synchronously with
* the given key and item. </p>
*
* @param key The key to add the item at.
* @param item The item to add.
*/
public synchronized Y put(T key, Y item) {
final int itemSize = getSize(item);
if (itemSize >= maxSize) {
onItemEvicted(key, item);
return null;
}
final Y result = cache.put(key, item);
if (item != null) {
currentSize += getSize(item);
}
if (result != null) {
// TODO: should we call onItemEvicted here?
currentSize -= getSize(result);
}
evict();
return result;
}
/**
* Removes the item at the given key and returns the removed item if present, and null otherwise.
*
* @param key The key to remove the item at.
*/
@Nullable
public synchronized Y remove(T key) {
final Y value = cache.remove(key);
if (value != null) {
currentSize -= getSize(value);
}
return value;
}
/**
* Clears all items in the cache.
*/
public void clearMemory() {
trimToSize(0);
}
/**
* Removes the least recently used items from the cache until the current size is less than the
* given size.
*
* @param size The size the cache should be less than.
*/
protected synchronized void trimToSize(int size) {
Map.Entry<T, Y> last;
while (currentSize > size) {
last = cache.entrySet().iterator().next();
final Y toRemove = last.getValue();
currentSize -= getSize(toRemove);
final T key = last.getKey();
cache.remove(key);
onItemEvicted(key, toRemove);
}
}
private void evict() {
trimToSize(maxSize);
}
}
什麼是LRU演算法? LRU是Least Recently Used的縮寫,即最近最少使用,主要基於這樣一個現實:在前面幾條指令中使用頻繁的資料很可能在後面的幾條指令中頻繁使用
LruCache採用的集合是LinkedHashMap,這個集合是HashMap的基礎上增加了 資料鏈表的功能,可以看到下面這個建構函式,第一個是初始容量100, 第二個是碰撞因子0.75(即真實容量到達總容量的75%就開始擴容),第三個是連結串列順序是否按訪問順序,關於這個容器的程式碼分析我們放在下一篇文章,在這裡我們只需要知道這個集合能記錄到你訪問資料的次序,最近的訪問的會放在連結串列的前面
private final LinkedHashMap<T, Y> cache = new LinkedHashMap<>(100, 0.75f, true);
initialMaxSize:初始大小,maxSize:最大,currentSize:當前 三個成員變數,建立時this.initialMaxSize 和this.maxSize 一樣。 注意setSizeMultiplier函式的作用是傳入一個變化乘數,改變當前的最大容量
private final int initialMaxSize;
private int maxSize;
private int currentSize = 0;
public LruCache(int size) {
this.initialMaxSize = size;
this.maxSize = size;
}
public synchronized void setSizeMultiplier(float multiplier) {
if (multiplier < 0) {
throw new IllegalArgumentException("Multiplier must be >= 0");
}
maxSize = Math.round(initialMaxSize * multiplier);
evict();
}
evict意思是驅逐,就是把資料清除出快取,把容量縮減到小於等於maxSize,while迴圈,處理當前大小大於入參的情況,取出連結串列中的一個,獲取其value的大小,清除,更新當前容器容量,直到符合要求
protected synchronized void trimToSize(int size) {
Map.Entry<T, Y> last;
while (currentSize > size) {
last = cache.entrySet().iterator().next();
final Y toRemove = last.getValue();
currentSize -= getSize(toRemove);
final T key = last.getKey();
cache.remove(key);
onItemEvicted(key, toRemove);
}
}
private void evict() {
trimToSize(maxSize);
}
獲取item的大小,預設現在是1 。比如要實現一個Bitmap 快取是需要返回大小的,快取的大小是取決於所有bitmap的總大小和,而不是總個數
protected int getSize(Y item) {
return 1;
}
一個清除元素髮生的回撥,讓LruCache 的繼承者選擇自己做要的事
protected void onItemEvicted(T key, Y item) {
// optional override
}
常規操作不解釋
public synchronized int getMaxSize() {
return maxSize;
}
public synchronized int getCurrentSize() {
return currentSize;
}
public synchronized boolean contains(T key) {
return cache.containsKey(key);
}
@Nullable
public synchronized Y get(T key) {
return cache.get(key);
}
put操作,首先獲取待加入的item 大小,如果大於快取最大容量,就不放進去,直接呼叫onItemEvicte. 小於快取最大容量,執行放入,item不為空,更新快取當前大小。 執行放入的結果result就是說如果之前在容器內key存在,會執行替換value的操作,這時候result!=null,需要把替換出來的item的大小減去, 作者弄了個//TODO,不知道這裡是否加上onItemEvicted 的回撥,個人感覺應該加上,畢竟資料被清除快取了,通知下,怎麼處理交給繼承者。 最後 evict(),因為單個待處理的item大小小於快取最大容量,但是加入後,有可能超出,這裡加個維護容量的程式碼
public synchronized Y put(T key, Y item) {
final int itemSize = getSize(item);
if (itemSize >= maxSize) {
onItemEvicted(key, item);
return null;
}
final Y result = cache.put(key, item);
if (item != null) {
currentSize += getSize(item);
}
if (result != null) {
// TODO: should we call onItemEvicted here?
currentSize -= getSize(result);
}
evict();
return result;
}
清除key出快取,常規操作,維護當前快取大小
public synchronized Y remove(T key) {
final Y value = cache.remove(key);
if (value != null) {
currentSize -= getSize(value);
}
return value;
}
讓快取容量小於等於0,起到clearMemory的作用
public void clearMemory() {
trimToSize(0);
}
分析完畢,整個範型類LruCache<T, Y> 程式碼很少,簡單的維護狀態,大小。依託於LinkedHashMap,完成了LRU記憶體快取的實現。