From 5408da630cb13a43de4557f0f02252d81c7bb973 Mon Sep 17 00:00:00 2001 From: Paul Lavoine Date: Thu, 14 Jun 2018 14:41:56 +0200 Subject: [PATCH 1/2] Enable to change clear cache option --- SDWebImage/SDImageCache.m | 24 ++++++++++++++++++++---- SDWebImage/SDImageCacheConfig.h | 17 +++++++++++++++++ SDWebImage/SDImageCacheConfig.m | 1 + 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 5f87891e..2833e6d7 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -606,7 +606,23 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { - (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock { dispatch_async(self.ioQueue, ^{ NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES]; - NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey]; + + // Compute content date key to be used for tests + NSURLResourceKey cacheContentDateKey = NSURLContentModificationDateKey; + switch (self.config.cacheClearBy) { + case SDImageCacheConfigExpireTypeAccessDate: + cacheContentDateKey = NSURLContentAccessDateKey; + break; + + case SDImageCacheConfigExpireTypeModificationDate: + cacheContentDateKey = NSURLContentModificationDateKey; + break; + + default: + break; + } + + NSArray *resourceKeys = @[NSURLIsDirectoryKey, cacheContentDateKey, NSURLTotalFileAllocatedSizeKey]; // This enumerator prefetches useful properties for our cache files. NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL @@ -633,12 +649,12 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { } // Remove files that are older than the expiration date; - NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey]; - if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) { + NSDate *modifiedDate = resourceValues[cacheContentDateKey]; + if ([[modifiedDate laterDate:expirationDate] isEqualToDate:expirationDate]) { [urlsToDelete addObject:fileURL]; continue; } - + // Store a reference to this file and account for its total size. NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey]; currentCacheSize += totalAllocatedSize.unsignedIntegerValue; diff --git a/SDWebImage/SDImageCacheConfig.h b/SDWebImage/SDImageCacheConfig.h index d3cb5421..1ec5e0ca 100644 --- a/SDWebImage/SDImageCacheConfig.h +++ b/SDWebImage/SDImageCacheConfig.h @@ -9,6 +9,17 @@ #import #import "SDWebImageCompat.h" +typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) { + /** + * When the image is accessed it will update this value + */ + SDImageCacheConfigExpireTypeAccessDate, + /** + * The image was obtained from the disk cache (Default) + */ + SDImageCacheConfigExpireTypeModificationDate +}; + @interface SDImageCacheConfig : NSObject /** @@ -49,4 +60,10 @@ */ @property (assign, nonatomic) NSUInteger maxCacheSize; +/** + * The attribute which the clear cache will be checked against when clearing the disk cache + * Default is Modified Date + */ +@property (assign, nonatomic) SDImageCacheConfigExpireType cacheClearBy; + @end diff --git a/SDWebImage/SDImageCacheConfig.m b/SDWebImage/SDImageCacheConfig.m index 923506d0..b146f265 100644 --- a/SDWebImage/SDImageCacheConfig.m +++ b/SDWebImage/SDImageCacheConfig.m @@ -21,6 +21,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week _diskCacheWritingOptions = NSDataWritingAtomic; _maxCacheAge = kDefaultCacheMaxCacheAge; _maxCacheSize = 0; + _cacheClearBy = SDImageCacheConfigExpireTypeModificationDate; } return self; } From 268d6d607aa7ec9ab65fb6df55e1ce4cb3d67493 Mon Sep 17 00:00:00 2001 From: Paul Lavoine Date: Sat, 23 Jun 2018 08:58:06 +0200 Subject: [PATCH 2/2] Refactor cacheClearBy property name --- SDWebImage/SDImageCache.m | 2 +- SDWebImage/SDImageCacheConfig.h | 2 +- SDWebImage/SDImageCacheConfig.m | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 2833e6d7..627420a5 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -609,7 +609,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { // Compute content date key to be used for tests NSURLResourceKey cacheContentDateKey = NSURLContentModificationDateKey; - switch (self.config.cacheClearBy) { + switch (self.config.diskCacheExpireType) { case SDImageCacheConfigExpireTypeAccessDate: cacheContentDateKey = NSURLContentAccessDateKey; break; diff --git a/SDWebImage/SDImageCacheConfig.h b/SDWebImage/SDImageCacheConfig.h index 1ec5e0ca..22c8f3e4 100644 --- a/SDWebImage/SDImageCacheConfig.h +++ b/SDWebImage/SDImageCacheConfig.h @@ -64,6 +64,6 @@ typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) { * The attribute which the clear cache will be checked against when clearing the disk cache * Default is Modified Date */ -@property (assign, nonatomic) SDImageCacheConfigExpireType cacheClearBy; +@property (assign, nonatomic) SDImageCacheConfigExpireType diskCacheExpireType; @end diff --git a/SDWebImage/SDImageCacheConfig.m b/SDWebImage/SDImageCacheConfig.m index b146f265..3061189e 100644 --- a/SDWebImage/SDImageCacheConfig.m +++ b/SDWebImage/SDImageCacheConfig.m @@ -21,7 +21,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week _diskCacheWritingOptions = NSDataWritingAtomic; _maxCacheAge = kDefaultCacheMaxCacheAge; _maxCacheSize = 0; - _cacheClearBy = SDImageCacheConfigExpireTypeModificationDate; + _diskCacheExpireType = SDImageCacheConfigExpireTypeModificationDate; } return self; }