本文首发于公众号“AntDream”,欢迎微信搜索“AntDream”或扫描文章底部二维码关注,和我一起每天进步一点点
Glide缓存
关联类:Engine、LruResourceCache、LruCache、ActiveResources
ActiveResources:弱引用缓存池
LruCache:LinkedHashMap缓存池
入口:Engine.load方法
先从缓存中取
LRU缓存池中取到EngineResource后,会从LRU缓存中删除,然后对它引用计数+1,放入弱引用池
缓存中没有找到,就需要创建任务执行
EngineResource通过引用计数来判断是否需要释放资源,释放的资源会从弱引用池中删除,放入LRU缓存中
缓存大小设置
涉及的类:MemorySizeCalculator
- 首先获取App可用内存大小,Glide的内存大小限制在0.4以下,如果是低内存的系统,则是在0.33
- 图片缓存大小,用几屏来表示,跟屏幕的分辨率有关
- LRU缓存动态限制图片缓存大小
DiskLruCache中的读写锁
写的时候会加锁,这个锁是自定义的,并且有一个锁的池子
每次写的时候会加锁,并且会对这个WriteLock的interestedThreads分别在开始写和结束时进行加减操作
writeLocker从锁池子里取,key的话是用请求的key做哈希得到
锁的缓存做了2级,一级是通过上面的key和锁放在一个HashMap中;
另一级是定义在内部类WriteLockPool的ArrayDeque里面,默认大小是10
取的时候先从HashMap中取,取不到再从WriteLockPool中取
释放锁的时候,会把writeLock的interestedThreads进行减一操作,如果为0了就释放锁,放入WriteLockPool中
欢迎关注我的公众号 AntDream查看更多精彩文章!