一般android会采用缓存机制,内存缓存采用LruCache,存储缓存采用DiskLruCache
LruCache内部维护了一个LinkedHashMap
/**
* @param maxSize for caches that do not override {@link #sizeOf}, this is
* the maximum number of entries in the cache. For all other caches,
* this is the maximum sum of the sizes of the entries in this cache.
*/
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);
}
同时是线程安全的
synchronized (this) {
mapValue = map.get(key);
if (mapValue != null) {
hitCount++;
return mapValue;
}
missCount++;
}
synchronized (this) {
createCount++;
mapValue = map.put(key, createdValue);
if (mapValue != null) {
// There was a conflict so undo that last put
map.put(key, mapValue);
} else {
size += safeSizeOf(key, createdValue);
}
}
一般存储空间为当前运行空间的1/8 单位为KB
LruCache 3.1以后源码可见
DiskLruCache 一般存储空间为50M key采用url经过Md5加密,防止url中的特殊字符,value为bitmap,对外提供get,put方法