一、Lrucache簡介
1. 什么是LruCache
LRU的全稱是Least Recently Used,即最近最少使用,LruCache 的實現(xiàn)原理就是把近期最少使用的數(shù)據(jù)從緩存中移除,保留使用最頻繁的數(shù)據(jù)。LruCache內(nèi)部采用的是LinkedHashMap,LruCache 作為內(nèi)存緩存,使用強引用方式緩存有限個數(shù)據(jù),當(dāng)緩存的某個數(shù)據(jù)被訪問時,它就會被移動到隊列的頭部,當(dāng)一個新數(shù)據(jù)要添加到LruCache而此時緩存大小要滿時,隊尾的數(shù)據(jù)就有可能會被垃圾回收器(GC)回收掉。
下面是LruCache類源碼文檔:
A cache that holds strong references to a limited number of values. Each time a value is accessed, it is moved to the head of a queue. When a value is added to a full cache, the value at the end of that queue is evicted and may become eligible for garbage collection.
2. LruCache常用方法
void resize(int maxSize) //更新存儲大小
V put(K key, V value) //存數(shù)據(jù),返回之前key對應(yīng)的value,如果沒有,返回null
V get(K key) //取出key對應(yīng)的緩存數(shù)據(jù)
V remove(K key) //移除key對應(yīng)的value
void evictAll() //清空緩存數(shù)據(jù)
Map<K, V> snapshot() //復(fù)制一份緩存并返回,順序從最近最少訪問到最多訪問排序
二、LruCache的簡單使用
下面通過一個下載網(wǎng)絡(luò)圖片進行緩存到本地的例子進行學(xué)習(xí),在第一次加載的時候是建立網(wǎng)絡(luò)連接進行下載,在下載之后再進行圖片顯示的時候利用已經(jīng)緩存的數(shù)據(jù)進行顯示。LruCache是內(nèi)存緩存,緩存的大小可以自己設(shè)置,通常設(shè)置為手機內(nèi)存的1/8。下面進行使用的操作:
1. 新建一個圖片加載類,用來對緩存進行操作,實例化LruCache類,并重寫sizeof方法
public class LruCacheTest {
private LruCache<String,Bitmap> mLruCache;
public void initLruCache(){
long maxMemory = Runtime.getRuntime ().maxMemory ();//手機的最大內(nèi)存
int cacheSize = (int) maxMemory/8;//設(shè)置緩存的大小
mLruCache = new LruCache<String, Bitmap> (cacheSize){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount ();
}
};
}
}
2. 創(chuàng)建下載圖片,緩存圖片的方法
// 把Bitmap對象加入到緩存中
public void saveBitmapToCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
lruCache.put(key, bitmap);
}
}
// 從緩存中得到Bitmap對象
public Bitmap getBitmapFromMemCache(String key) {
Log.i(TAG, "lrucache size: " + lruCache.size());
return lruCache.get(key);
}
// 從緩存中刪除指定的Bitmap
public void removeBitmapFromMemory(String key) {
lruCache.remove(key);
}
3. 新建一個內(nèi)部任務(wù)類進行線程池操作,在之前線程池的代碼上進行改進
public class ThreadPoolUtils {
/**
* 線程池大小
*/
private static final int CORE_POOL_SIZE = 4;
/**
* 最大線程數(shù)
*/
private static final int MAXIMUM_POOL_SIZE = 5;
/**
* 空閑線程存活時間為2秒
*/
private static final long KEEP_ALIVE_TIME = 2;
private static ThreadPoolUtils threadPoolUtils;
private ThreadPoolExecutor mThreadPoolExecutor;
private BlockingQueue<Runnable> mBlockingDeque;
private Context mContext;
private Handler handler;
public LruCache<String, Bitmap> imageLruCache;
private ThreadPoolUtils() {
}
public static ThreadPoolUtils getInstance() {
if(threadPoolUtils == null) {
synchronized (ThreadPoolUtils.class) {
if(threadPoolUtils == null) {
threadPoolUtils = new ThreadPoolUtils ();
}
}
}
return threadPoolUtils;
}
public void initThreadPool(Context context) {
this.mContext = context.getApplicationContext ();
mBlockingDeque = new LinkedBlockingDeque<> ();
handler = new Handler ();
mThreadPoolExecutor = new ThreadPoolExecutor (CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS, mBlockingDeque);
long maxMemory = Runtime.getRuntime ().maxMemory ();
int cacheSize = (int) (maxMemory / 8);
imageLruCache = new LruCache<String, Bitmap> (cacheSize) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getHeight () / 1024;
}
};
}
public void startThread(String imageUrl, ImageView iv) {
try {
ThreadPoolTask task = new ThreadPoolTask (imageUrl, iv);
mThreadPoolExecutor.execute (task);
} catch (Exception e) {
Log.e ("threadtest", "AbortPolicy...已超出規(guī)定的線程數(shù)量,不能再增加了....");
}
}
public void saveBitmapToCache(String key, Bitmap bitmap) {
if(getBitmapFromCache (key) == null) {
imageLruCache.put (key, bitmap);
Log.i ("cache size", "is " + imageLruCache.size ());
}
}
public Bitmap getBitmapFromCache(String key) {
return imageLruCache.get (key);
}
public void removeCache(String key) {
imageLruCache.remove (key);
}
public class ThreadPoolTask implements Runnable {
private String imageUrl;
private ImageView imageView;
private Bitmap bitmap;
public ThreadPoolTask(String url, ImageView iv) {
this.imageUrl = url;
this.imageView = iv;
}
@Override
public void run() {
boolean flag = true;
try {
while (flag) {
bitmap = downImage (imageUrl);
threadPoolUtils.saveBitmapToCache ("IdImage", bitmap);
Log.i ("CacheSize", "is" + threadPoolUtils.getBitmapFromCache ("IdImage").getByteCount ());
//圖片下載完成,handler通知主線程更新界面
if(handler != null) {
handler.post (new Runnable () {
@Override
public void run() {
imageView.setImageBitmap (bitmap);
MainActivity.lruCache = imageLruCache;
Toast.makeText (mContext, "Bitmap size is " + bitmap.getHeight (), Toast.LENGTH_SHORT).show ();
}
});
}
flag = false;
}
} catch (Exception e) {
e.printStackTrace ();
}
}
public Bitmap downImage(String imageUrl) {
InputStream inputStream = null;
Bitmap bitmap = null;
URL url;
HttpURLConnection connection = null;
try {
url = new URL (imageUrl);
connection = (HttpURLConnection) url.openConnection ();
connection.connect ();
inputStream = connection.getInputStream ();
bitmap = BitmapFactory.decodeStream (inputStream);
inputStream.close ();
} catch (IOException e) {
e.printStackTrace ();
} finally {
if(connection != null) {
connection.disconnect ();
}
if(inputStream != null) {
try {
inputStream.close ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
return bitmap;
}
}
}
4. 在需要進行圖片顯示的地方進行圖片下載和加載,第二次加載緩存里面的內(nèi)容
//首先判斷緩存里面是否有圖片
final Bitmap bitmap = MainActivity.lruCache.get ("IdImage");
if(bitmap == null) {
String imgUrl = "https://upload-images.jianshu.io/upload_images/13206622-5c1797f186484061.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/460";
threadPoolUtils.startThread (imgUrl, ivProfilePhoto);
} else {
ivProfilePhoto.setImageBitmap (bitmap);
Toast.makeText (getActivity (), "cache size is " + bitmap.getHeight (), Toast.LENGTH_SHORT).show ();
}