UsingFreqLimitedMemoryCache源码阅读



Universal-Image-Loader的内存缓存策略

1. 只使用的是强引用缓存

LruMemoryCache(这个类就是这个开源框架默认的内存缓存类,缓存的是bitmap的强引用)

2.使用强引用和弱引用相结合的缓存有

3.只使用弱引用缓存

WeakMemoryCache(这个类缓存bitmap的总大小没有限制,唯一不足的地方就是不稳定,缓存的图片容易被回收掉)

继承关系:

public class UsingFreqLimitedMemoryCache extends LimitedMemoryCachepublic abstract class LimitedMemoryCache extends BaseMemoryCachepublic abstract class BaseMemoryCache implements MemoryCachepublic interface MemoryCache extends MemoryCacheAware<String, Bitmap>@Deprecatedpublic interface MemoryCacheAware<K, V>

1、先来看MemoryCacheAware:

/* Interface for memory cache*/@Deprecatedpublic interface MemoryCacheAware<K, V> {/*Puts value into cache by key* @return true – if value was put into cache successfully;false – if value was not put into cache*/boolean put(K key, V value);/** Returns value by key. If there is no value for key then null will be returned. */V get(K key);/** Removes item by key */V remove(K key);/** Returns all keys of cache */Collection<K> keys();/** Remove all items from cache */void clear();}

MemoryCacheAware源码:

public interface MemoryCache extends MemoryCacheAware<String, Bitmap> {}

3、BaseMemoryCache源码:

/*为memory cache提供一些基本功能;提供object的引用(非强引用)存储*/public abstract class BaseMemoryCache implements MemoryCache {/** 存储objects的非强引用,Collections.synchronizedMap保证线程安全*/private final Map<String, Reference<Bitmap>> softMap = Collections.synchronizedMap(new HashMap<String, Reference<Bitmap>>());@Overridepublic Bitmap get(String key) {Bitmap result = null;Reference<Bitmap> reference = softMap.get(key);if (reference != null) {result = reference.get();}return result;}@Overridepublic boolean put(String key, Bitmap value) {softMap.put(key, createReference(value));return true;}@Overridepublic Bitmap remove(String key) {Reference<Bitmap> bmpRef = softMap.remove(key);return bmpRef == null ? null : bmpRef.get();}@Overridepublic Collection<String> keys() {synchronized (softMap) {return new HashSet<String>(softMap.keySet());}}@Overridepublic void clear() {softMap.clear();}/** Creates {@linkplain Reference not strong} reference of value */protected abstract Reference<Bitmap> createReference(Bitmap value);}

4、LimitedMemoryCache源码:

/** * 限定的Cache.提供Object的存储。所有存储的bitmap的总内存大小不超过限定值 * 注:该cache使用强引用和弱引用来存储Bitmaps; * 强引用——对于限额内的bitmaps * 弱应用——对于其他的Bitmaps */public abstract class LimitedMemoryCache extends BaseMemoryCache {private static final int MAX_NORMAL_CACHE_SIZE_IN_MB = 16;private static final int MAX_NORMAL_CACHE_SIZE = MAX_NORMAL_CACHE_SIZE_IN_MB * 1024 * 1024;private final int sizeLimit;/*AtomicInteger,一个提供原子操作的Integer的类,使得操作线程安全*/private final AtomicInteger cacheSize;/*** 包含存储objects的强引用。每个object都添加到最尾端;如果hard cache的大超过了限定值,首端的object将会被删除*(但它依然存在在softMap中,而且可以随时被GC回收)// 返回一个synchronizes封装的线程安全的Listpublic static <T> List<T> synchronizedList(List<T> list)*/private final List<Bitmap> hardCache = Collections.synchronizedList(new LinkedList<Bitmap>());/**构造函数:sizeLimit单位bytes */public LimitedMemoryCache(int sizeLimit) {this.sizeLimit = sizeLimit;cacheSize = new AtomicInteger();/**检测避免sizeLimit值设置过大 */if (sizeLimit > MAX_NORMAL_CACHE_SIZE) {L.w("You set too large memory cache size (more than %1$d Mb)", MAX_NORMAL_CACHE_SIZE_IN_MB);}}/**操作成功返回true,操作失败返回false先尝试将Bitmap添加至hard cache,再将其添加至soft cache*/@Overridepublic boolean put(String key, Bitmap value) {boolean putSuccessfully = false;int valueSize = getSize(value); //抽象函数,返回Bitmap的大小值int sizeLimit = getSizeLimit(); //返回sizeLimitint curCacheSize = cacheSize.get();//返回cacheSize的当前值/**如果添加的bitmap的size大于sizeLimit,,则直接不将其添加至hard cache*/if (valueSize < sizeLimit) {/**判断新添加的Object的valueSize加上当前cache中已有object的curCacheSize超过限定值,则会删除适当Bitmap*/while (curCacheSize + valueSize > sizeLimit) {Bitmap removedValue = removeNext(); //abstract函数,返回需要删除的下一个Bitmapif (hardCache.remove(removedValue)) {curCacheSize = cacheSize.addAndGet(-getSize(removedValue));}}hardCache.add(value);//添加到LinkedList<Bitmap>尾部cacheSize.addAndGet(valueSize);//即cacheSize+valueSizeputSuccessfully = true;}// Add value to soft cachesuper.put(key, value);return putSuccessfully;}@Overridepublic Bitmap remove(String key) {Bitmap value = super.get(key);if (value != null) {if (hardCache.remove(value)) {cacheSize.addAndGet(-getSize(value));}}return super.remove(key);}@Overridepublicvoid clear() {hardCache.clear();cacheSize.set(0);super.clear();}protected int getSizeLimit() {return sizeLimit;}protected abstract int getSize(Bitmap value);protected abstract Bitmap removeNext();}

5、UsingFreqLimitedMemoryCache源码:(关键在于get中更新使用频率)

空虚无聊的时候就读书,但一定得有自己的生活目标和计划。

UsingFreqLimitedMemoryCache源码阅读

相关文章:

你感兴趣的文章:

标签云: