How to update image in cache when image changed on server with SDWebImage

前端 未结 6 1558
感动是毒
感动是毒 2020-12-16 12:48

I am using SDWebImage library to download images from server. https://github.com/rs/SDWebImage

SDWebImage not able update the cached image when image updated on serv

6条回答
  •  执笔经年
    2020-12-16 13:13

    Update: I've actually written an entire guide about cache, including cache validation https://kean.github.io/blog/image-caching

    SDWebImage uses NSURLCache when you set SDWebImageRefreshCached option. Apple's URL loading system implements HTTP cache, including cached responses validation. HTTP cache is quite complex, however there are many beginners guides on HTTP caching:

    • How To Optimize Your Site With HTTP Caching
    • HTTP caching

    Basically, the server needs to include some of HTTP cache control headers in each response. There are many different strategies that can be used to implement revalidation. You may use either Last-Modified or ETag. This way each time the client sends a request it will automatically include in your request either Last-Modified or ETag value from previously cached response. If the image hasn't change the server will respond with status code 302 (not modified) and NSURLConnection/NSURLSession will transparently give you a cached response from NSURLCache. You don't have to download the data again, buy you still need to check with the server each time you make a request.

    You can also specify an expiration date using HTTP cache control. If the expiration mechanism is used, NSURLConnection/NSURLSession will not revalidate cached response until it is not expired.

    For more info about HTTP cache control see the links above. HTTP cache is a universal cache mechanism that should be used whenever possible.

    I would recommend to use Nuke framework for image loading (disclaimer: writted by me). It uses NSURLCache by default while still having a memory cache that holds decompressed images.

提交回复
热议问题