SDWebImageSwiftUI icon indicating copy to clipboard operation
SDWebImageSwiftUI copied to clipboard

Is it possible to programmatically update images in disk cache?

Open adams-family opened this issue 3 years ago • 3 comments

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!

adams-family avatar Jun 20 '22 07:06 adams-family

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:]

dreampiggy avatar Jun 20 '22 08:06 dreampiggy

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

adams-family avatar Jun 20 '22 10:06 adams-family

@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.

adams-family avatar Jun 20 '22 10:06 adams-family