首页 > 上传图片后服务器返回的url 的缓存问题

上传图片后服务器返回的url 的缓存问题

我现在的需求是,上传图片后,服务器给我返回一个url,如果已经上传过,那么检查缓存,直接返回url,如果不在缓存中,再post。如果已经上传,那么再上传服务器也是不会接受的。

现在问题是:系统自带的缓存策略是根据url 作为key 来缓存结果的,但是 上传图片的url 一直都是一样的,只有httpBody 才不一样,这样就导致无论上传多少图片,缓存的都是最后一次上传图片后的返回结果,那么只要开启缓存,无论上传什么图片,返回的结果都是一样的。

现在只有不开缓存,功能才正常,打开缓存,上传的图片返回的url 都是以一模一样的。

求大神解决。


不用担心,使用SDWebImage加载你的图片就行,即使是图片URL不变的情况也可以正确更新。具体用法截取Github上的文档。

Handle image refresh

SDWebImage does very aggressive caching by default. It ignores all kind of caching control header returned by the HTTP server and cache the returned images with no time restriction. It implies your images URLs are static URLs pointing to images that never change. If the pointed image happen to change, some parts of the URL should change accordingly.

If you don't control the image server you're using, you may not be able to change the URL when its content is updated. This is the case for Facebook avatar URLs for instance. In such 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];

文档上说明了原理,刷新缓存的话,就会先获取文件header信息判断图片有没有更新,只会消耗很小的性能和流量。
* 如果你不是一定要自己实现缓存,我强烈建议你换成SDWebImage它帮你把缓存什么的都做好了。
* 如果你自己做了图片缓存,不想使用别的库。判断图片有没有更新的原理已经说明了,具体实现就追踪一下SDWebImageRefreshCached代码把。

【热门文章】
【热门文章】