From 63c8d708d0bf82ceaf3a72e8efde85d37056e55e Mon Sep 17 00:00:00 2001 From: Tim Johnsen Date: Mon, 7 Oct 2024 09:56:28 -0700 Subject: [PATCH 1/4] Update last access time of images when loaded from disk. The OS doesn't automatically bump access time when reading files, so LRU would effectively be equivalent to "least recently downloaded" in SDWebImage. --- SDWebImage/Core/SDDiskCache.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/SDWebImage/Core/SDDiskCache.m b/SDWebImage/Core/SDDiskCache.m index f1ee2680..4eca6cff 100644 --- a/SDWebImage/Core/SDDiskCache.m +++ b/SDWebImage/Core/SDDiskCache.m @@ -71,13 +71,16 @@ static NSString * const SDDiskCacheExtendedAttributeName = @"com.hackemist.SDDis } NSData *data = [NSData dataWithContentsOfFile:filePath options:self.config.diskCacheReadingOptions error:nil]; if (data) { + [[NSURL fileURLWithPath:filePath] setResourceValue:[NSDate date] forKey:NSURLContentAccessDateKey error:nil]; return data; } // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name // checking the key with and without the extension - data = [NSData dataWithContentsOfFile:filePath.stringByDeletingPathExtension options:self.config.diskCacheReadingOptions error:nil]; + filePath = filePath.stringByDeletingPathExtension; + data = [NSData dataWithContentsOfFile:filePath options:self.config.diskCacheReadingOptions error:nil]; if (data) { + [[NSURL fileURLWithPath:filePath] setResourceValue:[NSDate date] forKey:NSURLContentAccessDateKey error:nil]; return data; } From e524ca0310003dfceb29bb9efcb07834b4f4fe36 Mon Sep 17 00:00:00 2001 From: Tim Johnsen Date: Mon, 7 Oct 2024 10:08:46 -0700 Subject: [PATCH 2/4] 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. --- SDWebImage/Core/SDDiskCache.m | 7 +++---- SDWebImage/Core/SDImageCacheConfig.h | 2 +- SDWebImage/Core/SDImageCacheConfig.m | 2 +- Tests/Tests/SDWebImageTestCache.m | 6 +++--- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/SDWebImage/Core/SDDiskCache.m b/SDWebImage/Core/SDDiskCache.m index 4eca6cff..938b844a 100644 --- a/SDWebImage/Core/SDDiskCache.m +++ b/SDWebImage/Core/SDDiskCache.m @@ -152,11 +152,8 @@ static NSString * const SDDiskCacheExtendedAttributeName = @"com.hackemist.SDDis NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES]; // Compute content date key to be used for tests - NSURLResourceKey cacheContentDateKey = NSURLContentModificationDateKey; + NSURLResourceKey cacheContentDateKey; switch (self.config.diskCacheExpireType) { - case SDImageCacheConfigExpireTypeAccessDate: - cacheContentDateKey = NSURLContentAccessDateKey; - break; case SDImageCacheConfigExpireTypeModificationDate: cacheContentDateKey = NSURLContentModificationDateKey; break; @@ -166,7 +163,9 @@ static NSString * const SDDiskCacheExtendedAttributeName = @"com.hackemist.SDDis case SDImageCacheConfigExpireTypeChangeDate: cacheContentDateKey = NSURLAttributeModificationDateKey; break; + case SDImageCacheConfigExpireTypeAccessDate: default: + cacheContentDateKey = NSURLContentAccessDateKey; break; } diff --git a/SDWebImage/Core/SDImageCacheConfig.h b/SDWebImage/Core/SDImageCacheConfig.h index 91889158..dace4beb 100644 --- a/SDWebImage/Core/SDImageCacheConfig.h +++ b/SDWebImage/Core/SDImageCacheConfig.h @@ -115,7 +115,7 @@ typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) { /* * 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; diff --git a/SDWebImage/Core/SDImageCacheConfig.m b/SDWebImage/Core/SDImageCacheConfig.m index 6e594eda..70402dbb 100644 --- a/SDWebImage/Core/SDImageCacheConfig.m +++ b/SDWebImage/Core/SDImageCacheConfig.m @@ -34,7 +34,7 @@ static const NSInteger kDefaultCacheMaxDiskAge = 60 * 60 * 24 * 7; // 1 week _diskCacheWritingOptions = NSDataWritingAtomic; _maxDiskAge = kDefaultCacheMaxDiskAge; _maxDiskSize = 0; - _diskCacheExpireType = SDImageCacheConfigExpireTypeModificationDate; + _diskCacheExpireType = SDImageCacheConfigExpireTypeAccessDate; _fileManager = nil; 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 diff --git a/Tests/Tests/SDWebImageTestCache.m b/Tests/Tests/SDWebImageTestCache.m index 336c1048..2f43539f 100644 --- a/Tests/Tests/SDWebImageTestCache.m +++ b/Tests/Tests/SDWebImageTestCache.m @@ -91,7 +91,7 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac - (void)removeExpiredData { NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxDiskAge]; NSURL *diskCacheURL = [NSURL fileURLWithPath:self.cachePath isDirectory:YES]; - NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLAttributeModificationDateKey]; + NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLAttributeContentAccessDateKey]; NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL includingPropertiesForKeys:resourceKeys options:NSDirectoryEnumerationSkipsHiddenFiles @@ -108,8 +108,8 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac } // Remove files that are older than the expiration date; - NSDate *modifiedDate = resourceValues[NSURLAttributeModificationDateKey]; - if (expirationDate && [[modifiedDate laterDate:expirationDate] isEqualToDate:expirationDate]) { + NSDate *accessDate = resourceValues[NSURLAttributeContentAccessDateKey]; + if (expirationDate && [[accessDate laterDate:expirationDate] isEqualToDate:expirationDate]) { [self.fileManager removeItemAtURL:fileURL error:nil]; } } From dcd0c46b7f770623df7082185bc07ad4cbbf2409 Mon Sep 17 00:00:00 2001 From: Tim Johnsen Date: Fri, 11 Oct 2024 20:21:03 -0700 Subject: [PATCH 3/4] Update SDWebImageTestCache.m Co-authored-by: DreamPiggy --- Tests/Tests/SDWebImageTestCache.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Tests/SDWebImageTestCache.m b/Tests/Tests/SDWebImageTestCache.m index 2f43539f..b01ce1e9 100644 --- a/Tests/Tests/SDWebImageTestCache.m +++ b/Tests/Tests/SDWebImageTestCache.m @@ -91,7 +91,7 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac - (void)removeExpiredData { NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxDiskAge]; NSURL *diskCacheURL = [NSURL fileURLWithPath:self.cachePath isDirectory:YES]; - NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLAttributeContentAccessDateKey]; + NSArray *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentAccessDateKey]; NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL includingPropertiesForKeys:resourceKeys options:NSDirectoryEnumerationSkipsHiddenFiles From 0cde1582d859756563a1191dc83010446c284543 Mon Sep 17 00:00:00 2001 From: Tim Johnsen Date: Fri, 11 Oct 2024 20:21:10 -0700 Subject: [PATCH 4/4] Update SDWebImageTestCache.m Co-authored-by: DreamPiggy --- Tests/Tests/SDWebImageTestCache.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Tests/SDWebImageTestCache.m b/Tests/Tests/SDWebImageTestCache.m index b01ce1e9..5e97cc39 100644 --- a/Tests/Tests/SDWebImageTestCache.m +++ b/Tests/Tests/SDWebImageTestCache.m @@ -108,7 +108,7 @@ static NSString * const SDWebImageTestDiskCacheExtendedAttributeName = @"com.hac } // Remove files that are older than the expiration date; - NSDate *accessDate = resourceValues[NSURLAttributeContentAccessDateKey]; + NSDate *accessDate = resourceValues[NSURLContentAccessDateKey]; if (expirationDate && [[accessDate laterDate:expirationDate] isEqualToDate:expirationDate]) { [self.fileManager removeItemAtURL:fileURL error:nil]; }