讲到图片请求,主要涉及到网络请求,内存缓存,硬盘缓存等原理和4大引用的问题,概括起来主要有以下几个内容:
原理示意图
主体有三个,分别是UI,缓存模块和数据源(网络)。它们之间的关系如下:
① UI:请求数据,使用唯一的Key值索引Memory Cache中的Bitmap。
② 内存缓存:缓存搜索,如果能找到Key值对应的Bitmap,则返回数据。否则执行第三步。
③ 硬盘存储:使用唯一Key值对应的文件名,检索SDCard上的文件。
④ 如果有对应文件,使用BitmapFactory.decode*方法,解码Bitmap并返回数据,同时将数据写入缓存。如果没有对应文件,执行第五步。
⑤ 下载图片:启动异步线程,从数据源下载数据(Web)。
⑥ 若下载成功,将数据同时写入硬盘和缓存,并将Bitmap显示在UI中。
UIL中的内存缓存策略
1. 只使用的是强引用缓存
LruMemoryCache(这个类就是这个开源框架默认的内存缓存类,缓存的是bitmap的强引用,下面我会从源码上面分析这个类)2.使用强引用和弱引用相结合的缓存有
UsingFreqLimitedMemoryCache(如果缓存的图片总量超过限定值,先删除使用频率最小的bitmap)
LRULimitedMemoryCache(这个也是使用的lru算法,和LruMemoryCache不同的是,他缓存的是bitmap的弱引用) FIFOLimitedMemoryCache(先进先出的缓存策略,当超过设定值,先删除最先加入缓存的bitmap) LargestLimitedMemoryCache(当超过缓存限定值,先删除最大的bitmap对象) LimitedAgeMemoryCache(当 bitmap加入缓存中的时间超过我们设定的值,将其删除)3.只使用弱引用缓存
WeakMemoryCache(这个类缓存bitmap的总大小没有限制,唯一不足的地方就是不稳定,缓存的图片容易被回收掉)
我们直接选择UIL中的默认配置缓存策略进行分析。
1.
ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(context);
ImageLoaderConfiguration.createDefault(…)这个方法最后是调用Builder.build()方法创建默认的配置参数的。默认的内存缓存实现是LruMemoryCache,磁盘缓存是UnlimitedDiscCache。
所以android在以后的版本中建议使用LruMemoryCache进行管理。
LruMemoryCache:一种使用强引用来保存有数量限制的Bitmap的cache(在空间有限的情况,保留最近使用过的Bitmap)。每次Bitmap被访问时,它就被移动到一个队列的头部。当Bitmap被添加到一个空间已满的cache时,在队列末尾的Bitmap会被挤出去并变成适合被GC回收的状态。 注意:这个cache只使用强引用来保存Bitmap。
LruMemoryCache实现MemoryCache,而MemoryCache继承自MemoryCacheAware。
1.
public
interface
MemoryCache
extends
MemoryCacheAware
下面给出继承关系图
@Override public ICache getBitmapCache() { if (bitmapCache == null) { bitmapCache = new ICache() { @Override public void remove(String key) { ImageLoader.getInstance().getMemoryCache().remove(key); } @Override public boolean put(String key, Bitmap value) { if (key == null || value == null) return false; return ImageLoader.getInstance().getMemoryCache().put(key, value); } @Override public Collection keys() { return null; } @Override public Bitmap get(String key) { return ImageLoader.getInstance().getMemoryCache().get(key); } @Override public void clear() { ImageLoader.getInstance().getMemoryCache().clear(); } }; } return bitmapCache; }
看到这里我们就清楚LruMemoryCache使用LinkedHashMap来缓存数据,在LinkedHashMap.get()方法执行后,LinkedHashMap中entry的顺序会得到调整。那么我们怎么保证最近使用的项不会被剔除呢?接下去,让我们看看LruMemoryCache.put(...)。
public final boolean put(String key, Bitmap value) { if(key != null && value != null) { synchronized(this) { this.size += this.sizeOf(key, value); Bitmap previous = (Bitmap)this.map.put(key, value); if(previous != null) { this.size -= this.sizeOf(key, previous); } } this.trimToSize(this.maxSize); return true; } else { throw new NullPointerException("key == null || value == null"); } }所以不难理解我们在用 Universal-Image-Loader做图片加载的时候,有时候图片缓存超过阈值的时候,会去重新重服务器加载了
private void trimToSize(int maxSize) { while(true) { synchronized(this) { if(this.size < 0 || this.map.isEmpty() && this.size != 0) { throw new IllegalStateException(this.getClass().getName() + ".sizeOf() is reporting inconsistent results!"); } if(this.size > maxSize && !this.map.isEmpty()) { Entry toEvict = (Entry)this.map.entrySet().iterator().next(); if(toEvict != null) { String key = (String)toEvict.getKey(); Bitmap value = (Bitmap)toEvict.getValue(); this.map.remove(key); this.size -= this.sizeOf(key, value); continue; } } return; } } }
这时候我们会有一个以为,为什么遍历一下就可以将使用最少的bitmap缓存给剔除,不会误删到最近使用的bitmap缓存吗?首先,我们要清楚,LruMemoryCache定义的最近使用是指最近用get或put方式操作到的bitmap缓存。其次,之前我们直到LruMemoryCache的get操作其实是通过其内部字段LinkedHashMap.get(...)实现的,当LinkedHashMap的accessOrder==true时,每一次get或put操作都会将所操作项(图中第3项)移动到链表的尾部(见下图,链表头被认为是最少使用的,链表尾被认为是最常使用的。),每一次操作到的项我们都认为它是最近使用过的,当内存不够的时候被剔除的优先级最低。需要注意的是一开始的LinkedHashMap链表是按插入的顺序构成的,也就是第一个插入的项就在链表头,最后一个插入的就在链表尾。假设只要剔除图中的1,2项就能让LruMemoryCache小于原先限定的大小,那么我们只要从链表头遍历下去(从1→最后一项)那么就可以剔除使用最少的项了。
至此,我们就知道了LruMemoryCache缓存的整个原理,包括他怎么put、get、剔除一个元素的的策略。接下去,我们要开始分析默认的磁盘缓存策略了。
UIL中的磁盘缓存策略
幸好UIL提供了几种常见的磁盘缓存策略,你也可以自己去扩展,可以根据他提供的几种缓存策略做进一步的缓存值的限制,
FileCountLimitedDiscCache(可以设定缓存图片的个数,当超过设定值,删除掉最先加入到硬盘的文件) LimitedAgeDiscCache(设定文件存活的最长时间,当超过这个值,就删除该文件) TotalSizeLimitedDiscCache(设定缓存bitmap的最大值,当超过这个值,删除最先加入到硬盘的文件) UnlimitedDiscCache(这个缓存类没有任何的限制)- UsingFreqLimitedMemoryCache(如果缓存的图片总量超过限定值,先删除使用频率最小的bitmap)
- LRULimitedMemoryCache(这个也是使用的lru算法,和LruMemoryCache不同的是,他缓存的是bitmap的弱引用)
- FIFOLimitedMemoryCache(先进先出的缓存策略,当超过设定值,先删除最先加入缓存的bitmap)
- LargestLimitedMemoryCache(当超过缓存限定值,先删除最大的bitmap对象)
- LimitedAgeMemoryCache(当 bitmap加入缓存中的时间超过我们设定的值,将其删除)
在UIL中有着比较完整的存储策略,根据预先指定的空间大小,使用频率(生命周期),文件个数的约束条件,都有着对应的实现策略。最基础的接口DiscCacheAware和抽象类BaseDiscCache
接下来我们看一些硬盘缓存的一些策略:接下来就给大家分析分析硬盘缓存的策略,这个框架也提供了几种常见的缓存策略,当然如果你觉得都不符合你的要求,你也可以自己去扩展
- FileCountLimitedDiscCache(可以设定缓存图片的个数,当超过设定值,删除掉最先加入到硬盘的文件)
- LimitedAgeDiscCache(设定文件存活的最长时间,当超过这个值,就删除该文件)
- TotalSizeLimitedDiscCache(设定缓存bitmap的最大值,当超过这个值,删除最先加入到硬盘的文件)
- UnlimitedDiscCache(这个缓存类没有任何的限制)
下面我们就来分析分析TotalSizeLimitedDiscCache的源码实现
- /*******************************************************************************
- * Copyright 2011-2013 Sergey Tarasevich
- *
- * 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.nostra13.universalimageloader.cache.disc.impl;
- import com.nostra13.universalimageloader.cache.disc.LimitedDiscCache;
- import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator;
- import com.nostra13.universalimageloader.core.DefaultConfigurationFactory;
- import com.nostra13.universalimageloader.utils.L;
- import java.io.File;
- /**
- * Disc cache limited by total cache size. If cache size exceeds specified limit then file with the most oldest last
- * usage date will be deleted.
- *
- * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
- * @see LimitedDiscCache
- * @since 1.0.0
- */
- public class TotalSizeLimitedDiscCache extends LimitedDiscCache {
- private static final int MIN_NORMAL_CACHE_SIZE_IN_MB = 2;
- private static final int MIN_NORMAL_CACHE_SIZE = MIN_NORMAL_CACHE_SIZE_IN_MB * 1024 * 1024;
- /**
- * @param cacheDir Directory for file caching. Important: Specify separate folder for cached files. It's
- * needed for right cache limit work.
- * @param maxCacheSize Maximum cache directory size (in bytes). If cache size exceeds this limit then file with the
- * most oldest last usage date will be deleted.
- */
- public TotalSizeLimitedDiscCache(File cacheDir, int maxCacheSize) {
- this(cacheDir, DefaultConfigurationFactory.createFileNameGenerator(), maxCacheSize);
- }
- /**
- * @param cacheDir Directory for file caching. Important: Specify separate folder for cached files. It's
- * needed for right cache limit work.
- * @param fileNameGenerator Name generator for cached files
- * @param maxCacheSize Maximum cache directory size (in bytes). If cache size exceeds this limit then file with the
- * most oldest last usage date will be deleted.
- */
- public TotalSizeLimitedDiscCache(File cacheDir, FileNameGenerator fileNameGenerator, int maxCacheSize) {
- super(cacheDir, fileNameGenerator, maxCacheSize);
- if (maxCacheSize sizeLimit) {
- int freedSize = removeNext();
- if (freedSize == INVALID_SIZE) break; // cache is empty (have nothing to delete)
- curCacheSize = cacheSize.addAndGet(-freedSize);
- }
- cacheSize.addAndGet(valueSize);
- Long currentTime = System.currentTimeMillis();
- file.setLastModified(currentTime);
- lastUsageDates.put(file, currentTime);
- }
- /**
- * 根据key生成文件
- */
- @Override
- public File get(String key) {
- File file = super.get(key);
- Long currentTime = System.currentTimeMillis();
- file.setLastModified(currentTime);
- lastUsageDates.put(file, currentTime);
- return file;
- }
- /**
- * 硬盘缓存的清理
- */
- @Override
- public void clear() {
- lastUsageDates.clear();
- cacheSize.set(0);
- super.clear();
- }
- /**
- * 获取最早加入的缓存文件,并将其删除
- */
- private int removeNext() {
- if (lastUsageDates.isEmpty()) {
- return INVALID_SIZE;
- }
- Long oldestUsage = null;
- File mostLongUsedFile = null;
- Set entries = lastUsageDates.entrySet();
- synchronized (lastUsageDates) {
- for (Entry entry : entries) {
- if (mostLongUsedFile == null) {
- mostLongUsedFile = entry.getKey();
- oldestUsage = entry.getValue();
- } else {
- Long lastValueUsage = entry.getValue();
- if (lastValueUsage
关注打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?


微信扫码登录