Update default disk cleaning method to use content access date.
This is possibly controversial, but I suspect most clients of SDWebImage would prefer files purged based on their last time of use instead of the date when they were downloaded.
This commit is contained in:
parent
63c8d708d0
commit
e524ca0310
|
@ -152,11 +152,8 @@ static NSString * const SDDiskCacheExtendedAttributeName = @"com.hackemist.SDDis
|
||||||
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
|
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
|
||||||
|
|
||||||
// Compute content date key to be used for tests
|
// Compute content date key to be used for tests
|
||||||
NSURLResourceKey cacheContentDateKey = NSURLContentModificationDateKey;
|
NSURLResourceKey cacheContentDateKey;
|
||||||
switch (self.config.diskCacheExpireType) {
|
switch (self.config.diskCacheExpireType) {
|
||||||
case SDImageCacheConfigExpireTypeAccessDate:
|
|
||||||
cacheContentDateKey = NSURLContentAccessDateKey;
|
|
||||||
break;
|
|
||||||
case SDImageCacheConfigExpireTypeModificationDate:
|
case SDImageCacheConfigExpireTypeModificationDate:
|
||||||
cacheContentDateKey = NSURLContentModificationDateKey;
|
cacheContentDateKey = NSURLContentModificationDateKey;
|
||||||
break;
|
break;
|
||||||
|
@ -166,7 +163,9 @@ static NSString * const SDDiskCacheExtendedAttributeName = @"com.hackemist.SDDis
|
||||||
case SDImageCacheConfigExpireTypeChangeDate:
|
case SDImageCacheConfigExpireTypeChangeDate:
|
||||||
cacheContentDateKey = NSURLAttributeModificationDateKey;
|
cacheContentDateKey = NSURLAttributeModificationDateKey;
|
||||||
break;
|
break;
|
||||||
|
case SDImageCacheConfigExpireTypeAccessDate:
|
||||||
default:
|
default:
|
||||||
|
cacheContentDateKey = NSURLContentAccessDateKey;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -115,7 +115,7 @@ typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The attribute which the clear cache will be checked against when clearing the disk cache
|
* The attribute which the clear cache will be checked against when clearing the disk cache
|
||||||
* Default is Modified Date
|
* Default is Access Date
|
||||||
*/
|
*/
|
||||||
@property (assign, nonatomic) SDImageCacheConfigExpireType diskCacheExpireType;
|
@property (assign, nonatomic) SDImageCacheConfigExpireType diskCacheExpireType;
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ static const NSInteger kDefaultCacheMaxDiskAge = 60 * 60 * 24 * 7; // 1 week
|
||||||
_diskCacheWritingOptions = NSDataWritingAtomic;
|
_diskCacheWritingOptions = NSDataWritingAtomic;
|
||||||
_maxDiskAge = kDefaultCacheMaxDiskAge;
|
_maxDiskAge = kDefaultCacheMaxDiskAge;
|
||||||
_maxDiskSize = 0;
|
_maxDiskSize = 0;
|
||||||
_diskCacheExpireType = SDImageCacheConfigExpireTypeModificationDate;
|
_diskCacheExpireType = SDImageCacheConfigExpireTypeAccessDate;
|
||||||
_fileManager = nil;
|
_fileManager = nil;
|
||||||
if (@available(iOS 10.0, tvOS 10.0, macOS 10.12, watchOS 3.0, *)) {
|
if (@available(iOS 10.0, tvOS 10.0, macOS 10.12, watchOS 3.0, *)) {
|
||||||
_ioQueueAttributes = DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL; // DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM
|
_ioQueueAttributes = DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL; // DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM
|
||||||
|
|
|
@ -91,7 +91,7 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac
|
||||||
- (void)removeExpiredData {
|
- (void)removeExpiredData {
|
||||||
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxDiskAge];
|
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxDiskAge];
|
||||||
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.cachePath isDirectory:YES];
|
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.cachePath isDirectory:YES];
|
||||||
NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLAttributeModificationDateKey];
|
NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLAttributeContentAccessDateKey];
|
||||||
NSDirectoryEnumerator<NSURL *> *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
|
NSDirectoryEnumerator<NSURL *> *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
|
||||||
includingPropertiesForKeys:resourceKeys
|
includingPropertiesForKeys:resourceKeys
|
||||||
options:NSDirectoryEnumerationSkipsHiddenFiles
|
options:NSDirectoryEnumerationSkipsHiddenFiles
|
||||||
|
@ -108,8 +108,8 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove files that are older than the expiration date;
|
// Remove files that are older than the expiration date;
|
||||||
NSDate *modifiedDate = resourceValues[NSURLAttributeModificationDateKey];
|
NSDate *accessDate = resourceValues[NSURLAttributeContentAccessDateKey];
|
||||||
if (expirationDate && [[modifiedDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
|
if (expirationDate && [[accessDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
|
||||||
[self.fileManager removeItemAtURL:fileURL error:nil];
|
[self.fileManager removeItemAtURL:fileURL error:nil];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue