iOS SDWebImage源码分析

1.SDWebImage 默认内存和磁盘都缓存的,所以它属于二级缓存机制的设计模式。
2.SDWebImage 默认缓存的时间是一周
static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
3.SDWebImage 清理缓存的时间是当程序退出到后台、或者被杀死的时候。

能够以时间清除磁盘的方法为

- (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock;
调用的时机为

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteOldFiles)
                                                     name:UIApplicationWillTerminateNotification object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgroundDeleteOldFiles)
                                                     name:UIApplicationDidEnterBackgroundNotification object:nil];
磁盘清理的原则?
首先、通过时间进行清理。(最后修改时间>一周)
然后、根据占据内存大小进行清理。(如果占据内存大于上限、则按时间排序、删除到上限的1/2。)
image.png

SDWebImage底层实现缓存机制,主要由三块组成
1、网络下载获取(当缓存中寻找不到的情况下->下载完之后会自动存储到好磁盘缓存中)
2、磁盘缓存获取
3、内存缓存获取

你可能感兴趣的:(iOS SDWebImage源码分析)