GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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.

Sign up for GitHub

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

Merged
dreampiggy merged 4 commits into SDWebImage:master from lonepalm:lru-upstream
Oct 12, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

Supports LRU eviction on disk cache, change the default expire type to accessDate instead of modificationDate #3759

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
63c8d70
Update last access time of images when loaded from disk.
timonus Oct 7, 2024
e524ca0
Update default disk cleaning method to use content access date.
timonus Oct 7, 2024
dcd0c46
Update SDWebImageTestCache.m
timonus Oct 12, 2024
0cde158
Update SDWebImageTestCache.m
timonus Oct 12, 2024
File filter
Filter by extension

Clear filters
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
Diff view

There are no files selected for viewing

12 changes: 7 additions & 5 deletions 12 SDWebImage/Core/SDDiskCache.m
Show comments
View file
Open in desktop
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

@dreampiggy dreampiggy Oct 8, 2024

Copy link
Copy Markdown
Contributor

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.

All reactions

@timonus timonus Oct 8, 2024

Copy link
Copy Markdown
Contributor Author

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.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
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 2 SDWebImage/Core/SDImageCacheConfig.h
Show comments
View file
Open in desktop
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 2 SDWebImage/Core/SDImageCacheConfig.m
Show comments
View file
Open in desktop
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 dreampiggy Oct 8, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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
All reactions
  • 👍 1 reaction
_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 6 Tests/Tests/SDWebImageTestCache.m
Show comments
View file
Open in desktop
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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.