标签:不同 gets link 不同的 boolean mem public max 存储
前面的两篇文章着重介绍的是磁盘缓存,这篇文章主要是解说一下内存缓存。对于内存缓存。也打算分两篇文章来进行解说。在这一篇文章中,我们主要是关注三个类,
MemoryCache、BaseMemoryCache以及LimitedMemoryCache。
首先我们先看一下内存缓存的接口MemoryCache。
put(String key, Bitmap value); Bitmap get(String key); Bitmap remove(String key); Collection<String> keys(); void clear();
接下来我们看实现内存缓存的接口的抽象类BaseMemoryCache。
与前面的文章一样,还是先从变量入手。
/** Stores not strong references to objects */ private final Map<String, Reference<Bitmap>> softMap = Collections.synchronizedMap(new HashMap<String, Reference<Bitmap>>());
略微的关注一下以下的方法
@Override
public Bitmap get(String key) {
Bitmap result = null;
Reference<Bitmap> reference = softMap.get(key);
if (reference != null) {
result = reference.get();
}
return result;
}
最后我们来看一下有限内存缓存空间的缓存类LimitedMemoryCache,从继承关系上来看。它是对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; private final AtomicInteger cacheSize; private final List<Bitmap> hardCache = Collections.synchronizedList(new LinkedList<Bitmap>());
拿当中的对象的存储的方法来分析一下:
public boolean put(String key, Bitmap value) {
boolean putSuccessfully = false;
// Try to add value to hard cache
int valueSize = getSize(value);
int sizeLimit = getSizeLimit();
int curCacheSize = cacheSize.get();
if (valueSize < sizeLimit) {
while (curCacheSize + valueSize > sizeLimit) {
Bitmap removedValue = removeNext();
if (hardCache.remove(removedValue)) {
curCacheSize = cacheSize.addAndGet(-getSize(removedValue));
}
}
hardCache.add(value);
cacheSize.addAndGet(valueSize);
putSuccessfully = true;
}
// Add value to soft cache
super.put(key, value);
return putSuccessfully;
}
Ok,关于图片的内存缓存的第一篇先说到这里,后面会继续分析。希望对大家有所帮助哦~
具体解说Android图片下载框架UniversialImageLoader之内存缓存(三)
标签:不同 gets link 不同的 boolean mem public max 存储
原文地址:http://www.cnblogs.com/zhchoutai/p/6884454.html