LruCache缓存图片研究小结

来源:互联网 发布:君子知怕的论证过程 编辑:程序博客网 时间:2024/05/29 19:49

上一篇研究了LinkedHashMap实现LRU策略,虽然通过上述方式来实现图片缓存可以优化内存的使用效率,但是这种方式也存在一些问题,例如,LinkedHashMap不是线程安全的,所以在操作时需要考虑线程安全问题。另外在缓存时,只能指定缓存数据条目的数量,不能指定缓存区的大小,如果需要缓存的图片都比较大,可能就会出现问题。。。。

其实在Android SDK 中已经为我们提供了一个实现LRU策略的cache类,LruCache类,这个类封装了LinkedHashMap并且解决了LinkedHashMap中存在的问题,今天我们以LruCache缓存图片为例研究下这个类。

有一点需要提前说明下,今天我们研究的是android.support.v4.util包中的LruCache类而不是android.util包中也有一个LruCache类,这点需要提前说明下,不然很容易蒙圈,因为这两个类中有些方法实现是不同的。

首先我们看一下LruCache的成员变量,代码如下

    private final LinkedHashMap<K, V> map;    private int size;//当前容量    private int maxSize;//最大容量    private int putCount;//put的次数     private int createCount;//create的次数     private int evictionCount;//回收次数    private int hitCount;//命中次数    private int missCount;//丢失的次数

再看下构造方法。

   public LruCache(int maxSize) {        if (maxSize <= 0) {            throw new IllegalArgumentException("maxSize <= 0");        }        this.maxSize = maxSize;        this.map = new LinkedHashMap<K, V>(0, 0.75f, true);    }

在构造方法中初始化了maxSize和LinkedHashMap,并且LinkedHashMap为按照访问顺序排序(此部分请参照上一篇文章)。maxSize即为最大容量。

前面提到LruCache可以指定缓存区大小,这怎么实现呢?。。其实实现起来很简单只要在初始化的时候重写LruCache提供的sizeOf方法即可,代码如下。

        int LRU_CACHE_SIZE = 4 * 1024 * 1024; //4MB        LruCache<String,Bitmap> lruCache=new LruCache<String,Bitmap>(LRU_CACHE_SIZE) {            @Override            protected int sizeOf(String key, Bitmap value) {                if (value != null)                    return value.getByteCount();                else                    return 0;            }        };

首先我们定义了LruCache的最大容量为4 * 1024 * 1024,即4M的空间,然后我们重写sizeOf方法,让这个方法返回Bitmap的字节数。为什么这样写就可以指定缓存区大大小呢?接下来我们看下比较重要的put和get方法,你就会明白了~

首先看下put方法的源码。

   public final V put(K key, V value) {        if (key == null || value == null) {            throw new NullPointerException("key == null || value == null");        }        V previous;        synchronized (this) {            putCount++;            size += safeSizeOf(key, value);            previous = map.put(key, value);            if (previous != null) {                size -= safeSizeOf(key, previous);            }        }        if (previous != null) {            entryRemoved(false, key, previous, value);        }        trimToSize(maxSize);        return previous;    }

在方法体中,我们发现当key或者value为空时会抛出异常,这部分和LinkedHashMap是不同的,说明LruCache类是不支持key或者value为空,并且在对map进行put操作时加了synchronized ,保证了线程安全。另外我们发现有如下操作

size += safeSizeOf(key, value);

这部分看上去像对size(当前容量)的累加,我们继续进入到safeSizeOf方法中

   private int safeSizeOf(K key, V value) {        int result = sizeOf(key, value);        if (result < 0) {            throw new IllegalStateException("Negative size: " + key + "=" + value);        }        return result;    }

在这个方法中看到了之前重写sizeOf方法,看到这里我想大家就明白了。我们之前重写sizeOf方法让它返回bitmap的字节数,而在put方法中会对每次put进来的bimap的字节数利用size进行累加,这样我们利用size和maxSize进行比较,就可以得知当前容量是否大于最大容量,当大于时就可以进行删除最近最少少用的资源的操作了。另外sizeOf默认情况下是返回1的,所以如果不重写sizeOf方法,LruCache也是进行“计数”的。

接着看put方法的代码,我们会发现如下操作。

       previous = map.put(key, value);       if (previous != null) {                size -= safeSizeOf(key, previous);       }

我们都知道,在对一个hashmap进行put操作的时候 ,如果put的key-value键值对中的key已经存在与map中,新put的value会覆盖旧value,并且会返回旧value。如果key不存在于map中,则会返回null。这里利用previous获取返回值,如果previous不为空,则当前容量会减去previous的大小,这部分比较好理解,其实就是防止同一个key对应新旧value的大小的重复叠加。

接着往下看,在previous不为空的时候,会调用 entryRemoved(false, key, previous, value) 方法,并且把previous传进去了,查看此方法,我们发现此方法并没有函数体,看来需要我们重写此方法。

protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {}

根据前面所讲,我们可以在初始化的时候重写这个方法,来释放掉一个key对应的旧value所占用的资源,代码如下。

 LruCache<String,Bitmap> lruCache=new LruCache<String,Bitmap>(LRU_CACHE_SIZE) {            @Override            protected int sizeOf(String key, Bitmap value) {                if (value != null)                    return value.getByteCount();                else                    return 0;            }            @Override            protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {                if(evicted==false&&oldValue!=null){                    oldValue.recycle();//释放bitmap资源                }            }        };

看到这里,可能会有疑问,在什么方法里会进行删除最近最少的操作呢?接着看put方法我们会发现trimToSize方法,答案其实就在这个方法里面。代码如下。

public void trimToSize(int maxSize) {        while (true) {//不断循环删除linkedHashMap首元素,也就是最近最少访问的条目,直到size小于最大容量或者map中已经没有数据            K key;            V value;            synchronized (this) {                if (size < 0 || (map.isEmpty() && size != 0)) {                    throw new IllegalStateException(getClass().getName()                            + ".sizeOf() is reporting inconsistent results!");                }                if (size <= maxSize || map.isEmpty()) {//直到当前容量小于最大容量                     break;                }                Map.Entry<K, V> toEvict = map.entrySet().iterator().next();//指向首元素                key = toEvict.getKey();                value = toEvict.getValue();                map.remove(key);//删除最近最少的entry                size -= safeSizeOf(key, value);                evictionCount++;            }            entryRemoved(true, key, value, null);        }    }

观察方法体我们发现,当当前容量大于最大容量时,会不断删除首元素即最近最少访问的元素,然后重新计算当前容量大小。只有在当前容量小于最大容量或者map中没有数据的时候才会break出去。在代码的最后也会调用一次entryRemoved(true, key, value, null)方法,并且把因为空间不足而删除的元素的key和value传递进去,不过第一参数为true有别与先前调用时传递的false,所以根据第一参数区别我们就可以进行一些不一样的操作。

protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {                if(evicted==false&&oldValue!=null){                    oldValue.recycle();                }if(evicted==true&&oldValue!=null){                    //TODO 根据key value 进行二级缓存                }            }

好了,put方法说明的比较详细,相信大家已经有了比较深的了解,接下来get方法我们加快点节奏。get方法源码如下。

public final V get(K key) {        if (key == null) {//key 不允许为空            throw new NullPointerException("key == null");        }        V mapValue;        synchronized (this) {            mapValue = map.get(key);            if (mapValue != null) {                hitCount++;                return mapValue;            }            missCount++;        }        V createdValue = create(key);//根据key进行新建        if (createdValue == null) {            return null;        }        synchronized (this) {            createCount++;            mapValue = map.put(key, createdValue);            if (mapValue != null) {//有返回值,说明key对应value已存在,需要进行重现赋值,即取消上一步操作                map.put(key, mapValue);            } else {                size += safeSizeOf(key, createdValue);            }        }        if (mapValue != null) {            entryRemoved(false, key, createdValue, mapValue);//可以释放刚创建的createdValue            return mapValue;        } else {            trimToSize(maxSize);            return createdValue;        }    }

观察代码,我们发现其实lrucache方法中主要是对linkedhashmap进行get操作,这部分不清除的同学可以看下上一篇关于linkedhashmap的研究总结。

我们重点研究下通过key获取到的value为空时的case。观察代码,如果如果出现上述情况,会新建一个createdValue,并且把createdValue put到map中,利用mapValue 获取put返回值,继续判断mapValue 是否为空,如果mapValue 不为空说明此时此key在map中存在对应的value,所以接下来需要进行重新赋值,让key对应的value为mapValue 而不是createdValue。如果mapValue为空,说明key对应的value确实为空,需要进行就是重新计算当前size的大小。在代码的最后会判断mapValue是否为空,如果mapValue不为空的时候,会调用entryRemoved方法,并把createdValue放在oldvalue的位置,因为此时createdValue所占的资源是无用的,所以我们可以在entryRemoved中释放createdValue。如果mapValue不为空又会进入到trimToSize方法中进行容量计算和删除“最近最少”。好了,get也方法讲完了,最后看下remove方法。

   public final V remove(K key) {        if (key == null) {            throw new NullPointerException("key == null");        }        V previous;        synchronized (this) {            previous = map.remove(key);            if (previous != null) {                size -= safeSizeOf(key, previous);            }        }        if (previous != null) {            entryRemoved(false, key, previous, null);        }        return previous;    }

观察代码,基本上和get和put有异曲同工之妙,在这里就不重复阐述了,大家自己阅读代码吧!~~~

比较重要的方法都讲完了,到此大家对lrucache应该有了一个全面的了解了吧~~接下来我们稍微总结下lrucache吧。

1.LruCache封装了LinkedHashMap,提供了LRU缓存的功能,并且在关键操作加了synchronized ,实现了线程安全。
2.LruCache提供了trimToSize方法,当容量不足时会自动删除最近最少访问的键值对。
3.LruCache提供了entryRemoved(boolean evicted, K key, V oldValue, V newValue)方法,通过重写这个方,结合put,get,remove方法我们可以做更多的事情。
3.LruCache不允许空键值;
5.LruCache提供了sizeof方法,重写这个方法可以实现指定缓存区大小,而不是像LinkedHashMap一样只能指定缓存条目数。

ok!就写到这了,欢迎大家一起研究交流!~~~

0 0
原创粉丝点击