1 缓存配置SDImageCacheConfig
关于NSURLContentAccessDateKey & NSURLContentModificationDateKey
看一下SDImageCacheConfig.h
2 磁盘缓存的清理时机
2.1 SDDiskCache关于缓存清理的方法
SDDiskCache类的缓存清理方法定义在SDDiskCache协议里。
- (void)removeAllData;
- (void)removeDataForKey:(NSString *)key;
- (void)removeExpiredData;
分别看看这三个方法的调用时机
removeAllData调用时机
主要是SDImageCachesManager接口调用
SDImageCachesManager方法clearWithCacheType:completion:是暴露的一个清理缓存的接口,type是缓存策略,这里可以说是清理策略:清理磁盘缓存、清理内存缓存、清理磁盘和内存缓存。
1、
SDImageCachesManager方法clearWithCacheType:completion:如果SDImageCachesManager单例只有一个caches,那么直接调用SDImageCache方法clearWithCacheType:completion:
SDImageCache方法clearWithCacheType:completion:
SDImageCache方法clearDiskOnCompletion:
2、
SDImageCachesManager方法clearWithCacheType:completion
SDImageCachesManager方法concurrentClearWithCacheType:completion:enumerator:operation:
SDImageCache方法clearWithCacheType:completion:
SDImageCache方法clearDiskOnCompletion:
3、
SDImageCachesManager方法clearWithCacheType:completion
SDImageCachesManager方法serialClearWithCacheType:completion:enumerator:operation:
SDImageCache方法clearWithCacheType:completion:
SDImageCache方法clearDiskOnCompletion:
removeDataForKey:调用时机
主要是SDImageCachesManager接口调用
1、
- (void)removeImageForKey:(NSString *)key cacheType:(SDImageCacheType)cacheType completion:(SDWebImageNoParamsBlock)completionBlock
-
(void)concurrentRemoveImageForKey:(NSString *)key cacheType:(SDImageCacheType)cacheType completion:(SDWebImageNoParamsBlock)completionBlock enumerator:(NSEnumerator<id<SDImageCache>> *)enumerator operation:(SDImageCachesManagerOperation *)operation
-
(void)serialRemoveImageForKey:(NSString *)key cacheType:(SDImageCacheType)cacheType completion:(SDWebImageNoParamsBlock)completionBlock enumerator:(NSEnumerator<id<SDImageCache>> *)enumerator
SDImageCache方法removeImageForKey:fromMemory:fromDisk:withCompletion:
2、
- (void)removeImageFromDiskForKey:(NSString *)key {
if (!key)
_removeImageFromDiskForKey:
removeExpiredData方法
1、
程序要被杀死时的回调UIApplicationWillTerminateNotification
- (void)applicationWillTerminate:(NSNotification *)notification;
- (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock
2、程序要进入后台时回调
UIApplicationDidEnterBackgroundNotification
- (void)applicationDidEnterBackground:(NSNotification *)notification
removeExpiredData在清理过期磁盘缓存的同时,还会去检查当前的磁盘缓存是否大于当前的最大磁盘缓存,如果大于,则将磁盘缓存释放到最大值的一半,剔除策略根据我们的配置,有两种:(1)访问时间;(2)更新时间。
即一种是按最久未访问方式剔除溢出磁盘缓存,另一种是根据最久未更新方式剔除溢出磁盘缓存。
2 内存缓存的清理
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
SDMemoryCache三个通知
[config addObserver:self forKeyPath:NSStringFromSelector(@selector(maxMemoryCost)) options:0 context:SDMemoryCacheContext];
[config addObserver:self forKeyPath:NSStringFromSelector(@selector(maxMemoryCount)) options:0 context:SDMemoryCacheContext];
// 内存警告,清理所有内存缓存
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveMemoryWarning:)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
- (void)didReceiveMemoryWarning:(NSNotification *)notification {
// Only remove cache, but keep weak cache
[super removeAllObjects];
}
注意一下didReceiveMemoryWarning:方法注释,只移除cache,但是保持弱引用(SDMemoryCache里面那个NSMapTable对象)
NSCache的totalCostLimit与countLimit属性限制了NSCache最缓存空间大小和最大缓存数默认都是0,内存缓存类SDMemoryCache继承自NSCache,实例化时默认没有设置相关值。
NSCache特性:
(1)在出现内存告警时,NSCache会清空内部的缓存。
(2)在cost>totalCostLimit或者count>countLimit会开启LRU策略删除部分缓存。
网友评论