Enable to change clear cache option
This commit is contained in:
parent
b05959dbcb
commit
5408da630c
|
@ -606,7 +606,23 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
|
||||||
- (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock {
|
- (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock {
|
||||||
dispatch_async(self.ioQueue, ^{
|
dispatch_async(self.ioQueue, ^{
|
||||||
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
|
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
|
||||||
NSArray<NSString *> *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<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, cacheContentDateKey, NSURLTotalFileAllocatedSizeKey];
|
||||||
|
|
||||||
// This enumerator prefetches useful properties for our cache files.
|
// This enumerator prefetches useful properties for our cache files.
|
||||||
NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
|
NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
|
||||||
|
@ -633,8 +649,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove files that are older than the expiration date;
|
// Remove files that are older than the expiration date;
|
||||||
NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey];
|
NSDate *modifiedDate = resourceValues[cacheContentDateKey];
|
||||||
if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
|
if ([[modifiedDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
|
||||||
[urlsToDelete addObject:fileURL];
|
[urlsToDelete addObject:fileURL];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,17 @@
|
||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
#import "SDWebImageCompat.h"
|
#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
|
@interface SDImageCacheConfig : NSObject
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,4 +60,10 @@
|
||||||
*/
|
*/
|
||||||
@property (assign, nonatomic) NSUInteger maxCacheSize;
|
@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
|
@end
|
||||||
|
|
|
@ -21,6 +21,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
|
||||||
_diskCacheWritingOptions = NSDataWritingAtomic;
|
_diskCacheWritingOptions = NSDataWritingAtomic;
|
||||||
_maxCacheAge = kDefaultCacheMaxCacheAge;
|
_maxCacheAge = kDefaultCacheMaxCacheAge;
|
||||||
_maxCacheSize = 0;
|
_maxCacheSize = 0;
|
||||||
|
_cacheClearBy = SDImageCacheConfigExpireTypeModificationDate;
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue