android缓存机制
16lz
2022-06-17
一般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(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方法
更多相关文章
- [置顶] 我的Android进阶之旅------>android异步加载图片显示,并且
- 【Android开源项目分析】android轻量级开源缓存框架——ASimpleC
- Android图片下载缓存库picasso解析
- Android图片下载缓存库picasso解析
- trinea博客地址
- android 4.4 webview加载部分https网页白屏
- Android(安卓)使用finalBitmap实现缓存读取
- android使用util工具包
- 【从头学android】在两个Activity之间实现界面切换