GeezerVison is showing github.com — Supports LRU eviction on disk cache, change the default expire type to accessDate instead of modificationDate by timonus · Pull Request #3759 · SDWebImage/SDWebImage
Supports LRU eviction on disk cache, change the default expire type to accessDate instead of modificationDate
#3759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.h
(1)
.m
(3)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
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];
Comment thread
There was a problem hiding this comment.
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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
Logged times are (1) time to read data, (2) time to write content access date, and (3) 2/1
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
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
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 ;
Comment thread
dreampiggy
Oct 8, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
timonus reacted with thumbs up emoji
_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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.