Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions SDWebImage/Core/SDDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ - (NSData *)dataForKey:(NSString *)key {
}
NSData *data = [NSData dataWithContentsOfFile:filePath options:self.config.diskCacheReadingOptions error:nil];
if (data) {
[[NSURL fileURLWithPath:filePath] setResourceValue:[NSDate date] forKey:NSURLContentAccessDateKey error:nil];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this time consuming ?

If so, need a extra if (self.config.diskCacheExpireType == SDImageCacheConfigExpireTypeAccessDate) guard

If not, it's OK

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is quite fast, significantly faster than the data read in my (very quick) testing.

Screenshot 2024-10-08 at 11 12 33 AM

Logged times are (1) time to read data, (2) time to write content access date, and (3) 2/1

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;
}

Expand Down Expand Up @@ -149,11 +152,8 @@ - (void)removeExpiredData {
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;
Expand All @@ -163,7 +163,9 @@ - (void)removeExpiredData {
case SDImageCacheConfigExpireTypeChangeDate:
cacheContentDateKey = NSURLAttributeModificationDateKey;
break;
case SDImageCacheConfigExpireTypeAccessDate:
default:
cacheContentDateKey = NSURLContentAccessDateKey;
break;
}

Expand Down
2 changes: 1 addition & 1 deletion SDWebImage/Core/SDImageCacheConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion SDWebImage/Core/SDImageCacheConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ - (instancetype)init {
_diskCacheWritingOptions = NSDataWritingAtomic;
_maxDiskAge = kDefaultCacheMaxDiskAge;
_maxDiskSize = 0;
_diskCacheExpireType = SDImageCacheConfigExpireTypeModificationDate;

@dreampiggy dreampiggy Oct 8, 2024

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok to change default value, but this PR will marked as Behavior Changes and I'll point this on release changelog

Actually I don't know why the original author use modifyas expire type, since it's not actually LRU (we usually don't write to same cache key image)

_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
Expand Down
6 changes: 3 additions & 3 deletions Tests/Tests/SDWebImageTestCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ - (void)removeDataForKey:(nonnull NSString *)key {
- (void)removeExpiredData {
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxDiskAge];
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.cachePath isDirectory:YES];
NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLAttributeModificationDateKey];
NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentAccessDateKey];
NSDirectoryEnumerator<NSURL *> *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
includingPropertiesForKeys:resourceKeys
options:NSDirectoryEnumerationSkipsHiddenFiles
Expand All @@ -108,8 +108,8 @@ - (void)removeExpiredData {
}

// Remove files that are older than the expiration date;
NSDate *modifiedDate = resourceValues[NSURLAttributeModificationDateKey];
if (expirationDate && [[modifiedDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
NSDate *accessDate = resourceValues[NSURLContentAccessDateKey];
if (expirationDate && [[accessDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
[self.fileManager removeItemAtURL:fileURL error:nil];
}
}
Expand Down