Skip to content

Commit e524ca0

Browse files
committed
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.
1 parent 63c8d70 commit e524ca0

File tree

4 files changed

+8
-9
lines changed

4 files changed

+8
-9
lines changed

SDWebImage/Core/SDDiskCache.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,8 @@ - (void)removeExpiredData {
152152
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
153153

154154
// Compute content date key to be used for tests
155-
NSURLResourceKey cacheContentDateKey = NSURLContentModificationDateKey;
155+
NSURLResourceKey cacheContentDateKey;
156156
switch (self.config.diskCacheExpireType) {
157-
case SDImageCacheConfigExpireTypeAccessDate:
158-
cacheContentDateKey = NSURLContentAccessDateKey;
159-
break;
160157
case SDImageCacheConfigExpireTypeModificationDate:
161158
cacheContentDateKey = NSURLContentModificationDateKey;
162159
break;
@@ -166,7 +163,9 @@ - (void)removeExpiredData {
166163
case SDImageCacheConfigExpireTypeChangeDate:
167164
cacheContentDateKey = NSURLAttributeModificationDateKey;
168165
break;
166+
case SDImageCacheConfigExpireTypeAccessDate:
169167
default:
168+
cacheContentDateKey = NSURLContentAccessDateKey;
170169
break;
171170
}
172171

SDWebImage/Core/SDImageCacheConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) {
115115

116116
/*
117117
* The attribute which the clear cache will be checked against when clearing the disk cache
118-
* Default is Modified Date
118+
* Default is Access Date
119119
*/
120120
@property (assign, nonatomic) SDImageCacheConfigExpireType diskCacheExpireType;
121121

SDWebImage/Core/SDImageCacheConfig.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ - (instancetype)init {
3434
_diskCacheWritingOptions = NSDataWritingAtomic;
3535
_maxDiskAge = kDefaultCacheMaxDiskAge;
3636
_maxDiskSize = 0;
37-
_diskCacheExpireType = SDImageCacheConfigExpireTypeModificationDate;
37+
_diskCacheExpireType = SDImageCacheConfigExpireTypeAccessDate;
3838
_fileManager = nil;
3939
if (@available(iOS 10.0, tvOS 10.0, macOS 10.12, watchOS 3.0, *)) {
4040
_ioQueueAttributes = DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL; // DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM

Tests/Tests/SDWebImageTestCache.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ - (void)removeDataForKey:(nonnull NSString *)key {
9191
- (void)removeExpiredData {
9292
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxDiskAge];
9393
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.cachePath isDirectory:YES];
94-
NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLAttributeModificationDateKey];
94+
NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLAttributeContentAccessDateKey];
9595
NSDirectoryEnumerator<NSURL *> *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
9696
includingPropertiesForKeys:resourceKeys
9797
options:NSDirectoryEnumerationSkipsHiddenFiles
@@ -108,8 +108,8 @@ - (void)removeExpiredData {
108108
}
109109

110110
// Remove files that are older than the expiration date;
111-
NSDate *modifiedDate = resourceValues[NSURLAttributeModificationDateKey];
112-
if (expirationDate && [[modifiedDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
111+
NSDate *accessDate = resourceValues[NSURLAttributeContentAccessDateKey];
112+
if (expirationDate && [[accessDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
113113
[self.fileManager removeItemAtURL:fileURL error:nil];
114114
}
115115
}

0 commit comments

Comments
 (0)