一、Lrucache简介
1. 什么是LruCache
LRU的全称是Least Recently Used,即最近最少使用,LruCache 的实现原理就是把近期最少使用的数据从缓存中移除,保留使用最频繁的数据。LruCache内部采用的是LinkedHashMap,LruCache 作为内存缓存,使用强引用方式缓存有限个数据,当缓存的某个数据被访问时,它就会被移动到队列的头部,当一个新数据要添加到LruCache而此时缓存大小要满时,队尾的数据就有可能会被垃圾回收器(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) //存数据,返回之前key对应的value,如果没有,返回null
V get(K key) //取出key对应的缓存数据
V remove(K key) //移除key对应的value
void evictAll() //清空缓存数据
Map snapshot() //复制一份缓存并返回,顺序从最近最少访问到最多访问排序
二、LruCache的简单使用
下面通过一个下载网络图片进行缓存到本地的例子进行学习,在第一次加载的时候是建立网络连接进行下载,在下载之后再进行图片显示的时候利用已经缓存的数据进行显示。LruCache是内存缓存,缓存的大小可以自己设置,通常设置为手机内存的1/8。下面进行使用的操作:
1. 新建一个图片加载类,用来对缓存进行操作,实例化LruCache类,并重写sizeof方法
public class LruCacheTest {
private LruCache mLruCache;
public void initLruCache(){
long maxMemory = Runtime.getRuntime ().maxMemory ();//手机的最大内存
int cacheSize = (int) maxMemory/8;//设置缓存的大小
mLruCache = new LruCache (cacheSize){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount ();
}
};
}
}
2. 创建下载图片,缓存图片的方法
// 把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. 新建一个内部任务类进行线程池操作,在之前线程池的代码上进行改进
public class ThreadPoolUtils {
/**
* 线程池大小
*/
private static final int CORE_POOL_SIZE = 4;
/**
* 最大线程数
*/
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 mBlockingDeque;
private Context mContext;
private Handler handler;
public LruCache 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 (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...已超出规定的线程数量,不能再增加了....");
}
}
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. 在需要进行图片显示的地方进行图片下载和加载,第二次加载缓存里面的内容
//首先判断缓存里面是否有图片
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 ();
}