简体   繁体   English

NSURLCache不起作用(使用Cache-Control = no-cache)

[英]NSURLCache doesn't work (with Cache-Control = no-cache)

I set up NSURLCache in my AppDelegate: 我在AppDelegate中设置了NSURLCache:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:50 * 1024 * 1024
                                                     diskCapacity:50 * 1024 * 1024
                                                         diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];

Then I'm trying to use it with cachePolicy:NSURLRequestReturnCacheDataElseLoad 然后我试图将其与cachePolicy:NSURLRequestReturnCacheDataElseLoad一起使用

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:getUrl
                                                       cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                   timeoutInterval:timeInterval];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];

But it doesn't work at all (it requests server every time and don't even try to use cache). 但这根本不起作用(它每次都请求服务器,甚至不尝试使用缓存)。

I checked the headers in the server response, here it is: 我检查了服务器响应中的标头,这里是:

"Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate";
"Content-Encoding" = gzip;
"Content-Type" = "application/json; charset=utf-8";
Date = "Sun, 29 Dec 2013 15:13:12 GMT";
Expires = "Fri, 01 Jan 1990 00:00:00 GMT";
P3P = "CP=\"NOI DSP COR NID ADMa OPTa OUR NOR\"";
Pragma = "no-cache";
Server = QRATOR;

From the Apple Documentation : Apple文档中
The NSURLRequestReturnCacheDataElseLoad cache policy causes the URL loading system to use cached data, ignoring its age or expiration date, and to load the data from the originating source only if there is no cached version. NSURLRequestReturnCacheDataElseLoad高速缓存策略使URL加载系统使用高速缓存的数据,而忽略其使用期限或到期日期,并且仅当没有高速缓存的版本时才从原始源加载数据。

My questions are: 我的问题是:

  1. Should it work (by work I mean to cache server response) with "Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate" in the response header ? 它应该在响应头中使用“ Cache-Control” =“ no-cache,no-store,max-age = 0,must-revalidate”来工作(我的意思是缓存服务器响应)吗?

  2. If not, what solution should I use to cache server answers ? 如果没有,我应该使用什么解决方案来缓存服务器答案? (I can't make any changes on the server side) (我无法在服务器端进行任何更改)

It's spelled out pretty clearly in your Cache-Control header: 在Cache-Control标头中清楚地阐明了这一点:

"Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate";

That tells the client NOT to cache the response in no uncertain terms... 这告诉客户端不要以不确定的方式缓存响应...

You can manually write object to cache: 您可以手动将对象写入缓存:

NSURLCache *sharedCache = [NSURLCache sharedURLCache];
[sharedCache storeCachedResponse:cachedResponse forRequest:request];

First you have to prepare cachedResponse , where you can modify headers by yourself. 首先,您必须准备cachedResponse ,在这里您可以自己修改标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 URLResponse已缓存,尽管响应的缓存控制标头设置为no-cache - URLResponse cached although response's cache-control header is set to no-cache 是否可以防止将带有响应标头`Cache-Control:Private`的文件缓存在NSURLCache中? - Would a file with a response header `Cache-Control:Private` be prevented from being cached in a NSURLCache? NSURLCache与NSURLSession一起不尊重:Cache-Control:max-age:86000,private,must-revalidate - NSURLCache, together with NSURLSession, does not respect: Cache-Control: max-age:86000, private, must-revalidate NSURLCache不起作用 - NSURLCache doesn't work Alamofire / NSURLSession缓存,带有专用的Cache-Control和max-age - Alamofire/NSURLSession caching with private Cache-Control and max-age 绕过http响应头Cache-Control:如何设置缓存过期? - Bypassing http response header Cache-Control: how to set cache expiration? Cache-Control:max-age不会使AFNetworking缓存响应 - Cache-Control: max-age does not make AFNetworking cache the response 更改NSURLCache内存和磁盘容量不起作用 - Changing NSURLCache memory and disk capacity doesn't work 如何使用NSURLSession和NSURLCache进行缓存。 不工作 - How to cache using NSURLSession and NSURLCache. Not working 如何使用NSURLCache缓存NSURLProtocol服务的内容 - How to use NSURLCache to cache content served by an NSURLProtocol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM