SDWebImage / SDWebImageSwiftUI

SwiftUI Image loading and Animation framework powered by SDWebImage
https://sdwebimage.github.io/SDWebImageSwiftUI
MIT License
2.14k stars 219 forks source link

Is it possible to programmatically update images in disk cache? #216

Open adams-family opened 2 years ago

adams-family commented 2 years ago

Hi @dreampiggy, great library!

I wanted to ask if it is possible to programmatically update images in disk cache in case that the original files have changed on the web? Maybe purge the cache once a week?

Thanks!

dreampiggy commented 2 years ago

Using SDWebImageRefreshCached may help with this. Which follows iOS system handling for HTTP E-Tag, etc.

And, you can actually update the image by yourself, get the cache path by -[SDWebImageManager cacheKeyForURL:], then update the disk cache using -[SDImageCache storeImageDataToDisk:forKey:]

adams-family commented 2 years ago

@dreampiggy Thanks, SDWebImageRefreshCached sounds like a perfect solution. Very nice!

adams-family commented 2 years ago

@dreampiggy Sorry for the multiple comments... I think I struggle using SDWebImageRefreshCached the right way.

I added this to my (SwiftUI) code:

   WebImage(url: url, options: .refreshCached)
    .onSuccess { image, data, cacheType in
        switch cacheType {
        case .all:
            print(imageUrl, "cache = all")
        case .disk:
            print(imageUrl, "cache = disk")
        case .memory:
            print(imageUrl, "cache = memory")
        case .none:
            print(imageUrl, "cache = none")
        default:
            print(imageUrl, "cache = unknown")
        }
    }

The Etag has changed on the file:

Before: image

After: image

However, the image does not change :( I still see "disk" being printed on my debug console.