您当前的位置: 首页 >  缓存

xiangzhihong8

暂无认证

  • 1浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

xiangzhihong8 发布时间:2016-04-01 11:56:18 ,浏览量:1

讲到图片请求,主要涉及到网络请求,内存缓存,硬盘缓存等原理和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进行管理。

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

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

view source print ?
1. public interface MemoryCache extends MemoryCacheAware

下面给出继承关系图

Android-Universal-Image-Loader.LruMemoryCache

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

@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(...)。
注意到代码第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
  在CODE上查看代码片 派生到我的代码片
  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. Important: 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. Important: 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  sizeLimit) {  
  58.             int freedSize = removeNext();  
  59.             if (freedSize == INVALID_SIZE) break; // cache is empty (have nothing to delete)  
  60.             curCacheSize = cacheSize.addAndGet(-freedSize);  
  61.         }  
  62.         cacheSize.addAndGet(valueSize);  
  63.   
  64.         Long currentTime = System.currentTimeMillis();  
  65.         file.setLastModified(currentTime);  
  66.         lastUsageDates.put(file, currentTime);  
  67.     }  
  68.   
  69.     /** 
  70.      * 根据key生成文件 
  71.      */  
  72.     @Override  
  73.     public File get(String key) {  
  74.         File file = super.get(key);  
  75.   
  76.         Long currentTime = System.currentTimeMillis();  
  77.         file.setLastModified(currentTime);  
  78.         lastUsageDates.put(file, currentTime);  
  79.   
  80.         return file;  
  81.     }  
  82.   
  83.     /** 
  84.      * 硬盘缓存的清理 
  85.      */  
  86.     @Override  
  87.     public void clear() {  
  88.         lastUsageDates.clear();  
  89.         cacheSize.set(0);  
  90.         super.clear();  
  91.     }  
  92.   
  93.       
  94.     /** 
  95.      * 获取最早加入的缓存文件,并将其删除 
  96.      */  
  97.     private int removeNext() {  
  98.         if (lastUsageDates.isEmpty()) {  
  99.             return INVALID_SIZE;  
  100.         }  
  101.         Long oldestUsage = null;  
  102.         File mostLongUsedFile = null;  
  103.           
  104.         Set entries = lastUsageDates.entrySet();  
  105.         synchronized (lastUsageDates) {  
  106.             for (Entry entry : entries) {  
  107.                 if (mostLongUsedFile == null) {  
  108.                     mostLongUsedFile = entry.getKey();  
  109.                     oldestUsage = entry.getValue();  
  110.                 } else {  
  111.                     Long lastValueUsage = entry.getValue();  
  112.                     if (lastValueUsage 
关注
打赏
1482932726
查看更多评论
立即登录/注册

微信扫码登录

0.0572s