Skip to content

Commit 0b10fcb

Browse files
authored
Merge pull request SDWebImage#3759 from lonepalm/lru-upstream
Fix issue causing disk cache eviction LRU to not function as expected.
2 parents d573278 + 0cde158 commit 0b10fcb

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

SDWebImage/Core/SDDiskCache.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,16 @@ - (NSData *)dataForKey:(NSString *)key {
7171
}
7272
NSData *data = [NSData dataWithContentsOfFile:filePath options:self.config.diskCacheReadingOptions error:nil];
7373
if (data) {
74+
[[NSURL fileURLWithPath:filePath] setResourceValue:[NSDate date] forKey:NSURLContentAccessDateKey error:nil];
7475
return data;
7576
}
7677

7778
// fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
7879
// checking the key with and without the extension
79-
data = [NSData dataWithContentsOfFile:filePath.stringByDeletingPathExtension options:self.config.diskCacheReadingOptions error:nil];
80+
filePath = filePath.stringByDeletingPathExtension;
81+
data = [NSData dataWithContentsOfFile:filePath options:self.config.diskCacheReadingOptions error:nil];
8082
if (data) {
83+
[[NSURL fileURLWithPath:filePath] setResourceValue:[NSDate date] forKey:NSURLContentAccessDateKey error:nil];
8184
return data;
8285
}
8386

@@ -149,11 +152,8 @@ - (void)removeExpiredData {
149152
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
150153

151154
// Compute content date key to be used for tests
152-
NSURLResourceKey cacheContentDateKey = NSURLContentModificationDateKey;
155+
NSURLResourceKey cacheContentDateKey;
153156
switch (self.config.diskCacheExpireType) {
154-
case SDImageCacheConfigExpireTypeAccessDate:
155-
cacheContentDateKey = NSURLContentAccessDateKey;
156-
break;
157157
case SDImageCacheConfigExpireTypeModificationDate:
158158
cacheContentDateKey = NSURLContentModificationDateKey;
159159
break;
@@ -163,7 +163,9 @@ - (void)removeExpiredData {
163163
case SDImageCacheConfigExpireTypeChangeDate:
164164
cacheContentDateKey = NSURLAttributeModificationDateKey;
165165
break;
166+
case SDImageCacheConfigExpireTypeAccessDate:
166167
default:
168+
cacheContentDateKey = NSURLContentAccessDateKey;
167169
break;
168170
}
169171

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, NSURLContentAccessDateKey];
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[NSURLContentAccessDateKey];
112+
if (expirationDate && [[accessDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
113113
[self.fileManager removeItemAtURL:fileURL error:nil];
114114
}
115115
}

0 commit comments

Comments
 (0)