Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to SDWebImage Cached Images in my app when the image file on the server changes?

I am using the SDWebImage library to cache web images in my app:

https://github.com/rs/SDWebImage/blob/master/README.md

Current Usage:

[imageView setImageWithURL:[NSURL URLWithString:profilePictureUrl] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

My question is what happens once the image has been cached and then a couple of days later that image file on the server has been updated with a new image?

At the moment my application is still displaying the cached image.

I can't see in any of the documentation on setting a cache timeout or something that recognises that the file size has changed.

If anyone has experience using this particular library then any help would be greatly appreciated.

Thanks in advance.

like image 404
fulvio Avatar asked May 19 '11 03:05

fulvio


People also ask

Does cache store images?

After opening an app or website for the first time, a cache stashes files, images, and other pertinent data on your device. Cached data is used to quickly load an app or website for every subsequent visit.

How does caching images work?

It reduces the time it takes for the user to view the images or Javascript or CSS files. This is because the user now accesses the file from his system instead of getting downloaded over the network. At the same time, caching also reduces the number of requests and data transfer from your servers.

Should images be cached?

Generally yes, images should be cached in at least memory, and depending on your app (how likely is it to be reused,etc) in memory and storage. If you want to support your 3rd point (displaying when offline), you need to do storage caching, and memory caching is optional but probably a good idea.

Are images automatically cached in browser?

does the browser cache images automatically? @Logan: Yes, the browser caches images automatically, provided your server sends the necessary headers to tell the browser it's safe to cache it. (These headers may also tell the browser the expiration time, which is part of your question.)


2 Answers

I had a look at the source code. It processes the setImageWithURL method like this:

  1. Ask the memory cache if the image is there, if yes return the image and don't go any further
  2. Ask the disk cache if the image is there, if yes return the image and don't go any further
  3. Try to download the image, return image on success else keep the placeholder image

There is no request sent to ask the remote server if there is a new version while there is something old on disk, like using ETags of the HTTP protocol.

Digging a bit deeper the cache time is set to a static value in SDImageCache.m

static NSInteger cacheMaxCacheAge = 60*60*24*7; // 1 week 

it cannot be changed with a setter.

So as long as the image in the cache is valid the SDWebImage lib won't download anything new. After a week it'll download your changed image.

like image 191
Nick Weaver Avatar answered Sep 24 '22 07:09

Nick Weaver


You can use options parameter.

Swift version:

imageView.sd_setImage(with: URL(string: URLWithString:profilePictureUrl),                       placeholderImage: UIImage(named: "placeholder"),                       options: .refreshCached,                       completed: nil) 

Objective-C version:

[imageView sd_setImageWithURL:[NSURL URLWithString:profilePictureUrl]              placeholderImage:[UIImage imageNamed:@"placeholder.png"]              options:SDWebImageRefreshCached              completed: nil]; 

Cheers!

like image 22
Pei Avatar answered Sep 21 '22 07:09

Pei