Skip to content

Conversation

@timonus
Copy link
Contributor

@timonus timonus commented Oct 7, 2024

New Pull Request Checklist

  • I have read and understood the CONTRIBUTING guide

  • I have read the Documentation

  • I have searched for a similar pull request in the project and found none

  • I have updated this branch with the latest master to avoid conflicts (via merge from master or rebase)

  • I have added the required tests to prove the fix/feature I am adding

  • I have updated the documentation (if necessary)

  • I have run the tests and they pass

  • I have run the lint and it passes (pod lib lint)

Pull Request Description

NSFileManager does not automatically increment access date when reading files off disk, which means that use of SDImageCacheConfigExpireTypeAccessDate is effectively identical to using SDImageCacheConfigExpireTypeCreationDate. This doesn't seem like the correct behavior on SDWebImage's part, and it isn't what's documented here. This PR makes it so that SDWebImage adjusts the file content access date when files are read off disk to address the issue.

I've also added a commit that updates the default cache behavior to SDImageCacheConfigExpireTypeAccessDate since I believe that's what's commonly expected. People generally want cache purging by time-of-use in my experience instead of using the time the file was downloaded at. If you strongly disagree I can omit that change!

@timonus timonus changed the title Fix issue causing SDWebImage disk cache eviction LRU to not function as expected. Fix issue causing disk cache eviction LRU to not function as expected. Oct 7, 2024
@timonus
Copy link
Contributor Author

timonus commented Oct 7, 2024

I have done this in my image loading library for quite a long time (though I used to use modification date) https://github.com/timonus/TJImageCache/blob/main/TJImageCache/TJImageCache.m#L196

@dreampiggy
Copy link
Contributor

Sounds great. I'll have a check later.

Actually these code was not changed at least 5 years...😂 So it's broken at the beginning ?

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

_diskCacheWritingOptions = NSDataWritingAtomic;
_maxDiskAge = kDefaultCacheMaxDiskAge;
_maxDiskSize = 0;
_diskCacheExpireType = SDImageCacheConfigExpireTypeModificationDate;
Copy link
Contributor

@dreampiggy dreampiggy Oct 8, 2024

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)

@dreampiggy dreampiggy added Behavior Changes cache Anything related to Cache System labels Oct 8, 2024
@dreampiggy dreampiggy added this to the 5.20.0 milestone Oct 8, 2024
@dreampiggy
Copy link
Contributor

dreampiggy commented Oct 9, 2024

Can you rebase and run the test cases again (or update the test case with your changes, since you change the default value)

The master branch fix the CI issue

The OS doesn't automatically bump access time when reading files, so LRU would effectively be equivalent to "least recently downloaded" in SDWebImage.
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.
@timonus
Copy link
Contributor Author

timonus commented Oct 9, 2024

Alright, rebased and linting seems to pass. It looks like unit tests are all failing to build but I don't think that's caused by the contents of this PR. I updated the one test that referenced modified date here.

@timonus
Copy link
Contributor Author

timonus commented Oct 11, 2024

@dreampiggy anything I can do to nudge this along? I'm unable to run the tests locally without installing Cocoapods, which I need to make other changes for and I'd rather not do. I don't think the tests are failing due to my change but if they are could you let me know?

@dreampiggy
Copy link
Contributor

The SDWebImageTestCache.m has compile error, seems I can fix this

timonus and others added 2 commits October 11, 2024 20:21
Co-authored-by: DreamPiggy <lizhuoli1126@126.com>
Co-authored-by: DreamPiggy <lizhuoli1126@126.com>
@timonus
Copy link
Contributor Author

timonus commented Oct 12, 2024

Ah, thank you! Sorry I didn't catch that.

@dreampiggy
Copy link
Contributor

Merge for now. But the release date of 5.20 may be delayed due to other break changes I want to added.

@dreampiggy dreampiggy merged commit 0b10fcb into SDWebImage:master Oct 12, 2024
@dreampiggy dreampiggy changed the title Fix issue causing disk cache eviction LRU to not function as expected. Supports LRU eviction on disk cache, change the default expire type to accessDate instead of modificationDate Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Behavior Changes cache Anything related to Cache System

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants