您的位置:首页 > 移动开发 > Android开发

android缓存机制

2016-10-25 10:30 281 查看
一般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方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息