android 圖片加載框架-內存緩存

概述

緩存可以提高圖片加載效率,針對數據源來自網絡的圖片,還可以減少帶寬。緩存一般情況分兩類:內存緩存、磁盤緩存。本章主要介紹內存緩存。

怎麼來擼一個內存緩存,hashmap?軟引用?大小限制?回收規則?一堆的基礎需求浮現在了腦海。當看了picasso,universal imageloader ,glide,fresco等圖片加載框架,發現內存緩存的實現基本一致,都是使用lrucache。(fresco內存緩存包含兩部分:未解碼的字節碼緩存和bitmap緩存)。

LRUCache實現原理

這個類的代碼不長,卻實現了上面描述一個內存緩存需要的所有功能。這裏面主要歸功於lrucache使用了一個具有充當緩存機制的核心引擎,這個類是linkedhashmap。關於linkedhashmap,查看源碼可以看到,本質上也是一個hashmap,具有hashmap所有的優點:查詢效率高,自動擴充容量。同時,在此基礎上增加了指針用來標記存儲元素之間的前後關係,每當從map中讀取數據的時候,會重新排序,將操作的元素挪動到鏈表的頭部。這樣linkedhashmap就具備了最近最少使用的特性。下面來具體看lrucache的代碼實現。以picasso源碼裏的實現作爲樣本。

LRUCache代碼分析

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.squareup.picasso;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.squareup.picasso.Utils.KEY_SEPARATOR;

/** A memory cache which uses a least-recently used eviction policy. 
內存緩存,最近最少使用清除規則*/
public class LruCache implements Cache {
  //使用LinkedHashMap作爲存儲器引擎
  final LinkedHashMap<String, Bitmap> map;
  //設定緩存最大值
  private final int maxSize;

  private int size;
  private int putCount;
  private int evictionCount;
  private int hitCount;
  private int missCount;

  /** Create a cache using an appropriate portion of the available RAM as the maximum size. */
  public LruCache(@NonNull Context context) {
   //計算默認使用的內存緩存大小,這個大小在不同圖片
   //加載框架不太一樣,picasso默認使用app可分配最大內存
   //的15%,具體可以查看Utils類。
    this(Utils.calculateMemoryCacheSize(context));
  }

  /** Create a cache with a given maximum size in bytes. */
  public LruCache(int maxSize) {
    if (maxSize <= 0) {
      throw new IllegalArgumentException("Max size must be positive.");
    }
    this.maxSize = maxSize;
    this.map = new LinkedHashMap<>(0, 0.75f, true);
  }

  @Override public Bitmap get(@NonNull String key) {
    if (key == null) {
      throw new NullPointerException("key == null");
    }

    Bitmap mapValue;
    synchronized (this) {
      mapValue = map.get(key);
      if (mapValue != null) {
        hitCount++;
        return mapValue;
      }
      missCount++;
    }

    return null;
  }

  @Override public void set(@NonNull String key, @NonNull Bitmap bitmap) {
    if (key == null || bitmap == null) {
      throw new NullPointerException("key == null || bitmap == null");
    }

    int addedSize = Utils.getBitmapBytes(bitmap);
    if (addedSize > maxSize) {
      return;
    }

    synchronized (this) {
      putCount++;
      size += addedSize;
      Bitmap previous = map.put(key, bitmap);
      //查看hashmap源碼,previous表示key之前對應的value。
      //當key重複,插入新元素以後,會將之前元素替換掉。
      //因此此處需要將此前對應value的佔用大小減除
      if (previous != null) {
        size -= Utils.getBitmapBytes(previous);
      }
    }
    //每次添加元素,都要重新調整一下緩存的大小。
    trimToSize(maxSize);
  }

  private void trimToSize(int maxSize) {
    while (true) {
      String key;
      Bitmap 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<String, Bitmap> toEvict = map.entrySet().iterator().next();
        key = toEvict.getKey();
        value = toEvict.getValue();
        map.remove(key);
        size -= Utils.getBitmapBytes(value);
        evictionCount++;
      }
    }
  }

  /** Clear the cache. */
  public final void evictAll() {
    trimToSize(-1); // -1 will evict 0-sized elements
  }

  @Override public final synchronized int size() {
    return size;
  }

  @Override public final synchronized int maxSize() {
    return maxSize;
  }

  @Override public final synchronized void clear() {
    evictAll();
  }

  @Override public final synchronized void clearKeyUri(String uri) {
    int uriLength = uri.length();
    for (Iterator<Map.Entry<String, Bitmap>> i = map.entrySet().iterator(); i.hasNext();) {
      Map.Entry<String, Bitmap> entry = i.next();
      String key = entry.getKey();
      Bitmap value = entry.getValue();
      int newlineIndex = key.indexOf(KEY_SEPARATOR);
      if (newlineIndex == uriLength && key.substring(0, newlineIndex).equals(uri)) {
        i.remove();
        size -= Utils.getBitmapBytes(value);
      }
    }
  }

  /** Returns the number of times {@link #get} returned a value. */
  public final synchronized int hitCount() {
    return hitCount;
  }

  /** Returns the number of times {@link #get} returned {@code null}. */
  public final synchronized int missCount() {
    return missCount;
  }

  /** Returns the number of times {@link #set(String, Bitmap)} was called. */
  public final synchronized int putCount() {
    return putCount;
  }

  /** Returns the number of values that have been evicted. */
  public final synchronized int evictionCount() {
    return evictionCount;
  }
}

總結

Lrucache使用很少的代碼量,解決了一個內存緩存需要的訴求:
1. 存儲方案,使用map,查找效率高
2. 容量控制,計算app在當前設備所能分配的最大容量,取15%(可調整)
3. 回收策略,最近最少使用,這個策略通過linkedhashmap鏈表動態調整元素順序來實現。
這要歸功於java語言及其生態環境、開源社區強大的組件庫。正如牛頓說,我看的遠是因爲站在巨人的肩膀上。

思考:
爲什麼沒有使用軟引用?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章