首页 > 编程语言 >Universal-Image-Loader源码分析,及常用的缓存策略

Universal-Image-Loader源码分析,及常用的缓存策略

时间:2022-12-16 20:07:04浏览次数:60  
标签:缓存 key Universal cache Loader 源码 file Bitmap public


讲到图片请求,主要涉及到网络请求,内存缓存,硬盘缓存等原理和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中的默认配置缓存策略进行分析。


​view source​​​ ​​​print​​​ ​​​?​


​1.​

​ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(context);​

ImageLoaderConfiguration.createDefault(…)这个方法最后是调用Builder.build()方法创建默认的配置参数的。默认的内存缓存实现是LruMemoryCache,磁盘缓存是UnlimitedDiscCache。

所以android在以后的版本中建议使用LruMemoryCache进行管理。

一种使用强引用来保存有数量限制的Bitmap的cache(在空间有限的情况,保留最近使用过的Bitmap)。每次Bitmap被访问时,它就被移动到一个队列的头部。当Bitmap被添加到一个空间已满的cache时,在队列末尾的Bitmap会被挤出去并变成适合被GC回收的状态。
注意:这个cache只使用强引用来保存Bitmap。

LruMemoryCache实现MemoryCache,而MemoryCache继承自MemoryCacheAware。


​view source​​​ ​​​print​​​ ​​​?​


​1.​

​public​​​​interface​​ ​​MemoryCache ​​​​extends​​ ​​MemoryCacheAware<String, Bitmap>​

下面给出继承关系图


通过跟踪

LruMemoryCache.get()的代码我们发现,LruMemoryCache声称保留在空间有限的情况下保留最近使用过的Bitmap,这是一个LinkedHashMap<String, Bitmap>,Icache的源码缓存的代码如下:

@Override
    public ICache<String, Bitmap> getBitmapCache() {
        if (bitmapCache == null) {
            bitmapCache = new ICache<String, Bitmap>() {
                @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<String> 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(...)。


注意到代码第8行中的size+= sizeOf(key, value),这个size是什么呢?我们注意到在第19行有一个trimToSize(maxSize),trimToSize(...)这个函数就是用来限定LruMemoryCache的大小不要超过用户限定的大小,cache的大小由用户在LruMemoryCache刚开始初始化的时候限定。





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做图片加载的时候,有时候图片缓存超过阈值的时候,会去重新重服务器加载了

当Bitmap缓存的大小超过原来设定的maxSize时应该是在trimToSize(...)这个函数中做到的。这个函数做的事情也简单,遍历map,将多余的项(代码中对应toEvict)剔除掉,直到当前cache的大小等于或小于限定的大小。


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的源码实现


[java]  ​​view plain​​  ​​copy​​

 

​​​ ​​​

  1. /*******************************************************************************
  2.  * Copyright 2011-2013 Sergey Tarasevich
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *******************************************************************************/  
  16. package com.nostra13.universalimageloader.cache.disc.impl;  
  17.   
  18. import com.nostra13.universalimageloader.cache.disc.LimitedDiscCache;  
  19. import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator;  
  20. import com.nostra13.universalimageloader.core.DefaultConfigurationFactory;  
  21. import com.nostra13.universalimageloader.utils.L;  
  22.   
  23. import java.io.File;  
  24.   
  25. /**
  26.  * Disc cache limited by total cache size. If cache size exceeds specified limit then file with the most oldest last
  27.  * usage date will be deleted.
  28.  *
  29.  * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
  30.  * @see LimitedDiscCache
  31.  * @since 1.0.0
  32.  */  
  33. public class TotalSizeLimitedDiscCache extends LimitedDiscCache {  
  34.   
  35. private static final int MIN_NORMAL_CACHE_SIZE_IN_MB = 2;  
  36. private static final int MIN_NORMAL_CACHE_SIZE = MIN_NORMAL_CACHE_SIZE_IN_MB * 1024 * 1024;  
  37.   
  38. /**
  39.      * @param cacheDir     Directory for file caching. <b>Important:</b> Specify separate folder for cached files. It's
  40.      *                     needed for right cache limit work.
  41.      * @param maxCacheSize Maximum cache directory size (in bytes). If cache size exceeds this limit then file with the
  42.      *                     most oldest last usage date will be deleted.
  43.      */  
  44. public TotalSizeLimitedDiscCache(File cacheDir, int maxCacheSize) {  
  45. this(cacheDir, DefaultConfigurationFactory.createFileNameGenerator(), maxCacheSize);  
  46.     }  
  47.   
  48. /**
  49.      * @param cacheDir          Directory for file caching. <b>Important:</b> Specify separate folder for cached files. It's
  50.      *                          needed for right cache limit work.
  51.      * @param fileNameGenerator Name generator for cached files
  52.      * @param maxCacheSize      Maximum cache directory size (in bytes). If cache size exceeds this limit then file with the
  53.      *                          most oldest last usage date will be deleted.
  54.      */  
  55. public TotalSizeLimitedDiscCache(File cacheDir, FileNameGenerator fileNameGenerator, int maxCacheSize) {  
  56. super(cacheDir, fileNameGenerator, maxCacheSize);  
  57. if (maxCacheSize < MIN_NORMAL_CACHE_SIZE) {  
  58. "You set too small disc cache size (less than %1$d Mb)", MIN_NORMAL_CACHE_SIZE_IN_MB);  
  59.         }  
  60.     }  
  61.   
  62. @Override  
  63. protected int getSize(File file) {  
  64. return (int) file.length();  
  65.     }  
  66. }  

这个类是继承LimitedDiscCache,除了两个构造函数之外,还重写了getSize()方法,返回文件的大小,接下来我们就来看看LimitedDiscCache


[java]  ​​view plain​​  ​​copy​​

 

​​​ ​​​

  1. /*******************************************************************************
  2.  * Copyright 2011-2013 Sergey Tarasevich
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *******************************************************************************/  
  16. package com.nostra13.universalimageloader.cache.disc;  
  17.   
  18. import com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator;  
  19. import com.nostra13.universalimageloader.core.DefaultConfigurationFactory;  
  20.   
  21. import java.io.File;  
  22. import java.util.Collections;  
  23. import java.util.HashMap;  
  24. import java.util.Map;  
  25. import java.util.Map.Entry;  
  26. import java.util.Set;  
  27. import java.util.concurrent.atomic.AtomicInteger;  
  28.   
  29. /**
  30.  * Abstract disc cache limited by some parameter. If cache exceeds specified limit then file with the most oldest last
  31.  * usage date will be deleted.
  32.  *
  33.  * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
  34.  * @see BaseDiscCache
  35.  * @see FileNameGenerator
  36.  * @since 1.0.0
  37.  */  
  38. public abstract class LimitedDiscCache extends BaseDiscCache {  
  39.   
  40. private static final int INVALID_SIZE = -1;  
  41.   
  42. //记录缓存文件的大小  
  43. private final AtomicInteger cacheSize;  
  44. //缓存文件的最大值  
  45. private final int sizeLimit;  
  46. private final Map<File, Long> lastUsageDates = Collections.synchronizedMap(new HashMap<File, Long>());  
  47.   
  48. /**
  49.      * @param cacheDir  Directory for file caching. <b>Important:</b> Specify separate folder for cached files. It's
  50.      *                  needed for right cache limit work.
  51.      * @param sizeLimit Cache limit value. If cache exceeds this limit then file with the most oldest last usage date
  52.      *                  will be deleted.
  53.      */  
  54. public LimitedDiscCache(File cacheDir, int sizeLimit) {  
  55. this(cacheDir, DefaultConfigurationFactory.createFileNameGenerator(), sizeLimit);  
  56.     }  
  57.   
  58. /**
  59.      * @param cacheDir          Directory for file caching. <b>Important:</b> Specify separate folder for cached files. It's
  60.      *                          needed for right cache limit work.
  61.      * @param fileNameGenerator Name generator for cached files
  62.      * @param sizeLimit         Cache limit value. If cache exceeds this limit then file with the most oldest last usage date
  63.      *                          will be deleted.
  64.      */  
  65. public LimitedDiscCache(File cacheDir, FileNameGenerator fileNameGenerator, int sizeLimit) {  
  66. super(cacheDir, fileNameGenerator);  
  67. this.sizeLimit = sizeLimit;  
  68. new AtomicInteger();  
  69.         calculateCacheSizeAndFillUsageMap();  
  70.     }  
  71.   
  72. /**
  73.      * 另开线程计算cacheDir里面文件的大小,并将文件和最后修改的毫秒数加入到Map中
  74.      */  
  75. private void calculateCacheSizeAndFillUsageMap() {  
  76. new Thread(new Runnable() {  
  77. @Override  
  78. public void run() {  
  79. int size = 0;  
  80.                 File[] cachedFiles = cacheDir.listFiles();  
  81. if (cachedFiles != null) { // rarely but it can happen, don't know why  
  82. for (File cachedFile : cachedFiles) {  
  83. //getSize()是一个抽象方法,子类自行实现getSize()的逻辑  
  84.                         size += getSize(cachedFile);  
  85. //将文件的最后修改时间加入到map中  
  86.                         lastUsageDates.put(cachedFile, cachedFile.lastModified());  
  87.                     }  
  88.                     cacheSize.set(size);  
  89.                 }  
  90.             }  
  91.         }).start();  
  92.     }  
  93.   
  94. /**
  95.      * 将文件添加到Map中,并计算缓存文件的大小是否超过了我们设置的最大缓存数
  96.      * 超过了就删除最先加入的那个文件
  97.      */  
  98. @Override  
  99. public void put(String key, File file) {  
  100. //要加入文件的大小  
  101. int valueSize = getSize(file);  
  102.           
  103. //获取当前缓存文件大小总数  
  104. int curCacheSize = cacheSize.get();  
  105. //判断是否超过设定的最大缓存值  
  106. while (curCacheSize + valueSize > sizeLimit) {  
  107. int freedSize = removeNext();  
  108. if (freedSize == INVALID_SIZE) break; // cache is empty (have nothing to delete)  
  109.             curCacheSize = cacheSize.addAndGet(-freedSize);  
  110.         }  
  111.         cacheSize.addAndGet(valueSize);  
  112.   
  113.         Long currentTime = System.currentTimeMillis();  
  114.         file.setLastModified(currentTime);  
  115.         lastUsageDates.put(file, currentTime);  
  116.     }  
  117.   
  118. /**
  119.      * 根据key生成文件
  120.      */  
  121. @Override  
  122. public File get(String key) {  
  123. super.get(key);  
  124.   
  125.         Long currentTime = System.currentTimeMillis();  
  126.         file.setLastModified(currentTime);  
  127.         lastUsageDates.put(file, currentTime);  
  128.   
  129. return file;  
  130.     }  
  131.   
  132. /**
  133.      * 硬盘缓存的清理
  134.      */  
  135. @Override  
  136. public void clear() {  
  137.         lastUsageDates.clear();  
  138. 0);  
  139. super.clear();  
  140.     }  
  141.   
  142.       
  143. /**
  144.      * 获取最早加入的缓存文件,并将其删除
  145.      */  
  146. private int removeNext() {  
  147. if (lastUsageDates.isEmpty()) {  
  148. return INVALID_SIZE;  
  149.         }  
  150. null;  
  151. null;  
  152.           
  153.         Set<Entry<File, Long>> entries = lastUsageDates.entrySet();  
  154. synchronized (lastUsageDates) {  
  155. for (Entry<File, Long> entry : entries) {  
  156. if (mostLongUsedFile == null) {  
  157.                     mostLongUsedFile = entry.getKey();  
  158.                     oldestUsage = entry.getValue();  
  159. else {  
  160.                     Long lastValueUsage = entry.getValue();  
  161. if (lastValueUsage < oldestUsage) {  
  162.                         oldestUsage = lastValueUsage;  
  163.                         mostLongUsedFile = entry.getKey();  
  164.                     }  
  165.                 }  
  166.             }  
  167.         }  
  168.   
  169. int fileSize = 0;  
  170. if (mostLongUsedFile != null) {  
  171. if (mostLongUsedFile.exists()) {  
  172.                 fileSize = getSize(mostLongUsedFile);  
  173. if (mostLongUsedFile.delete()) {  
  174.                     lastUsageDates.remove(mostLongUsedFile);  
  175.                 }  
  176. else {  
  177.                 lastUsageDates.remove(mostLongUsedFile);  
  178.             }  
  179.         }  
  180. return fileSize;  
  181.     }  
  182.   
  183. /**
  184.      * 抽象方法,获取文件大小
  185.      * @param file
  186.      * @return
  187.      */  
  188. protected abstract int getSize(File file);  



最后看一些我们需要怎么使用

配置:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(YmatouApplication.instance())
.threadPriority(Thread.NORM_PRIORITY - 1)
.memoryCache(new LruMemoryCache(getMemoryCacheSize()))
.diskCacheSize(50 * ByteConstants.MB)
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.diskCacheFileCount(200)
.memoryCacheExtraOptions(480, 800)
.threadPoolSize(3)
.memoryCache(new WeakMemoryCache())
.tasksProcessingOrder(QueueProcessingType.FIFO)
.defaultDisplayImageOptions(getSimpleOptions().build())
.build();
ImageLoader.getInstance().init(config);

在做网络请求的时候,我们也做些配置,如网络请求之前默认什么背景,请求完成后,请求失败后怎么显示我们都可以在

defaultDisplayImageOptions(DisplayImageOptions.Builder builed)进行配置


private static DisplayImageOptions.Builder getSimpleOptions() {
DisplayImageOptions.Builder options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.imageScaleType(ImageScaleType.EXACTLY)
.showImageOnLoading(R.drawable.image_loading)
.showImageForEmptyUri(R.drawable.image_default)
.showImageOnFail(R.drawable.image_failed)
.resetViewBeforeLoading(true)
.bitmapConfig(Bitmap.Config.ARGB_8888);
return options;
}

加载图片:

public static void imageloader(String url, ImageView imageView) {
displayImage(url, imageView, null);
}




标签:缓存,key,Universal,cache,Loader,源码,file,Bitmap,public
From: https://blog.51cto.com/u_13657808/5948280

相关文章