当服务器上的图像文件发生更改时,我的应用程序中的 SDWebImage 缓存图像会发生什么情况?

发布于 2024-11-08 11:44:28 字数 540 浏览 4 评论 0原文

我正在使用 SDWebImage 库在我的应用程序中缓存 Web 图像:

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

当前使用情况:

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

我的问题是,一旦图像被缓存,几天后该图像文件会发生什么服务器已更新为新图像?

目前我的应用程序仍在显示缓存的图像。

我在任何文档中都看不到有关设置缓存超时或识别文件大小已更改的内容。

如果有人有使用这个特定库的经验,那么我们将不胜感激。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

夜深人未静 2024-11-15 11:44:28

我看了一下源代码。它处理 setImageWithURL 方法如下:

  1. 询问内存缓存图像是否存在,如果存在则返回图像,并且不再继续
  2. 询问磁盘缓存图像是否存在,如果存在则返回图像并不再继续
  3. 尝试下载图像,成功时返回图像,否则保留占位符图像

没有发送请求来询问远程服务器是否有新版本,而磁盘上有旧版本,例如使用HTTP 协议的 ETag

深入挖掘一下,缓存时间在 SDImageCache.m 中设置为静态值,

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

无法使用 setter 进行更改。

因此,只要缓存中的图像有效,SDWebImage lib 就不会下载任何新内容。一周后,它将下载您更改的图像。

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.

み格子的夏天 2024-11-15 11:44:28

您可以使用 options 参数。

Swift 版本:

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

Objective-C 版本:

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

干杯!

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!

生生漫 2024-11-15 11:44:28

SDImageCache 老化(现在有一个设置器:maxCacheAge)的问题是 SDWebImage 从未真正主动地对其执行任何操作。您需要在某个时候自己调用 cleanDisk 以从缓存中清除旧数据。注意:当应用程序终止时,SDWebImage 确实会调用 cleanDisk,但不能保证应用程序从操作系统获得终止通知。

The problem with SDImageCache's aging (which now has a setter: maxCacheAge) is that SDWebImage never really proactively does anything with it. You need to invoke cleanDisk yourself at some point to purge old data from the cache. Note: SDWebImage does invoke cleanDisk when the app terminates, but apps are not guaranteed to get a termination notification from the OS.

说谎友 2024-11-15 11:44:28
    NSURL *imageUrl = nil;
    NSDate *lastUpdate = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastUpdate"];
    NSDate *currentDate = [NSDate date];

    if (lastUpdate == nil 
        || ![lastUpdate isKindOfClass:[NSDate class]] 
        || [currentDate timeIntervalSinceDate:lastUpdate] > 60 * 60 *24) {
            [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"lastUpdate"];
            NSString *urlString = [NSString stringWithFormat:@"http://yourdomain.com/image/image.png?%f", [currentDate timeIntervalSince1970]];
            imageUrl = [NSURL URLWithString:urlString];
    }
    NSURL *imageUrl = nil;
    NSDate *lastUpdate = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastUpdate"];
    NSDate *currentDate = [NSDate date];

    if (lastUpdate == nil 
        || ![lastUpdate isKindOfClass:[NSDate class]] 
        || [currentDate timeIntervalSinceDate:lastUpdate] > 60 * 60 *24) {
            [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"lastUpdate"];
            NSString *urlString = [NSString stringWithFormat:@"http://yourdomain.com/image/image.png?%f", [currentDate timeIntervalSince1970]];
            imageUrl = [NSURL URLWithString:urlString];
    }
峩卟喜欢 2024-11-15 11:44:28

如果您想更改 Swift 中的默认缓存持续时间。在您的 AppDelegate 中设置它。

版本 3:

SDWebImageManager.sharedManager().imageCache.maxCacheAge = CACHE_TIME_IN_SECONDS

版本 4:

SDWebImageManager.shared().imageCache?.config.maxCacheAge = CACHE_TIME_IN_SECONDS

-

我相信这只会影响设置此值后缓存的图像。 IE,如果您的应用缓存具有默认缓存过期时间的图像,然后将其更改为其他内容,您的图像仍然只会在一周后过期。对此的一个简单解决方案是清除缓存。

Incase you are looking to change the default cache duration in Swift. Set this in your AppDelegate.

Version 3:

SDWebImageManager.sharedManager().imageCache.maxCacheAge = CACHE_TIME_IN_SECONDS

Version 4:

SDWebImageManager.shared().imageCache?.config.maxCacheAge = CACHE_TIME_IN_SECONDS

-

I believe this only effects images that are caches after this value is set. IE if your app cache an image with the default cache expiration and then change it something else, your images will still only expire after a week. An easily solution to this is to just clear your cache.

甜是你 2024-11-15 11:44:28

以下是我所观察到的。

  1. 如果图像名称/路径相同,SDWebImage 将在 1 周内不会再次下载它。

  2. 无论图像名称如何,SDWebImage 都会在 1 周后重新下载图像(从下载时间算起)。

    静态 NSInteger cacheMaxCacheAge = 60*60*24*7; // 1 周

  3. 他们有一个数据库,其中存储所有图像 URL。对于他们来说,图像 URL 就像主键(唯一键)。

所以基本上他们所做的是,如果 URL 发生更改并且数据库中不存在,则下载它。

从我的角度来看,他们所做的事情是正确的。前任。如果您为用户 A 上传图像,则必须更改图像名称并更改图像名称。这是基本的。我知道一些开发人员更喜欢图像名称相同(就像 userA.png 一样)。

Below is what I have observed.

  1. If the image name/ path is same, SDWebImage will not download it again for 1 week.

  2. Irrespective of Image name, SDWebImage will re-download the image after 1 week (from the time it is downloaded).

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

  3. They have one Database where all images URL are stored. For them, image URL is like primary key (unique key).

So basically what they do is if the URL is changed and not present in DB, download it.

From my point of view what they are doing is RIGHT. Ex. If you upload image let's say for user A, the image name has to be changed & this is basic. I know some developer prefer image name to be same (like userA.png always).

℡Ms空城旧梦 2024-11-15 11:44:28

SDWebImage 默认情况下会进行积极的缓存。但现在他们提供了尊重 HTTP 缓存控制标头并获取最新图像的选项。

为此,他们有一个新方法,您可以在选项中传递 SDWebImageRefreshCached

[imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/xyz/picture"]
             placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
                      options:SDWebImageRefreshCached];

您可以找到完整的方法详细信息和说明

SDWebImage does aggressive caching by default. But now they give the option to respect your HTTP caching control headers and get the latest image.

For this they have a new method where in options you can pass SDWebImageRefreshCached

[imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/xyz/picture"]
             placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
                      options:SDWebImageRefreshCached];

You can find the complete method details and explanation here.

生生不灭 2024-11-15 11:44:28

最新的 Swift 3.0* 和 SDWebImage

SDWebImageManager.shared().imageCache?.deleteOldFiles(completionBlock: nil)

Latest Swift 3.0* and SDWebImage

SDWebImageManager.shared().imageCache?.deleteOldFiles(completionBlock: nil)
滥情哥ㄟ 2024-11-15 11:44:28

SDWebImage 流程:

1) SDWebImage 缓存从服务器检索的图像

2) SDWebImage 使用 url 作为从缓存获取图像的键

3) SDWebImage检查:

是否能够从缓存中获取图像 -

如果则确定(例如网址已更改) - 无法从缓存中获取图像

,因此实际上,您将获取空 ImageView 且 SDWebImage 必须再次从服务器检索图像

SDWebImage flow:

1) SDWebImage caches image retrieved from the server

2) SDWebImage uses url is a key to the get image from cache

3) SDWebImage checks:

if it is able to get the image from cache - OK

if no (e.g url was changed) - not able to get the image from cache

so actually, you will get empty ImageView(s) and SDWebImage must retrieve images from the server again

秋意浓 2024-11-15 11:44:28

当我在 git 的 SDWeb 自述文件中读到时,
它可能会有所帮助:

根据您的情况,您可以使用 SDWebImageRefreshCached 标志。这会稍微降低性能,但会尊重 HTTP 缓存控制标头:

[imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]
                 placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
                          options:SDWebImageRefreshCached];

As I read in the SDWeb readme in git,
It might help:

In your case, you may use the SDWebImageRefreshCached flag. This will slightly degrade the performance but will respect the HTTP caching control headers:

[imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]
                 placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
                          options:SDWebImageRefreshCached];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文