Ios NSURLCache不';t工作(带缓存控制=无缓存)

Ios NSURLCache不';t工作(带缓存控制=无缓存),ios,caching,nsurlconnection,nsurlcache,Ios,Caching,Nsurlconnection,Nsurlcache,我在AppDelegate中设置了NSURLCache: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:50 * 1024 * 1024

我在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];
然后我尝试将它与cachePolicy一起使用:NSURLRequestReturnCacheDataElseLoad

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:getUrl
                                                       cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                   timeoutInterval:timeInterval];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
但它根本不起作用(它每次都请求服务器,甚至不尝试使用缓存)

我检查了服务器响应中的标题,如下所示:

"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;
从:
NSURLRequestReturnCacheDataElseLoad缓存策略导致URL加载系统使用缓存数据,忽略其年龄或过期日期,并且仅在没有缓存版本时才从原始源加载数据

我的问题是:

  • 在响应头中的“cache Control”=“no cache,no store,max age=0,must revalidate”时,它是否应该工作(我指的是缓存服务器响应)

  • 如果不是,我应该使用什么解决方案来缓存服务器答案?(我无法在服务器端进行任何更改)


  • 它在您的缓存控制头中清楚地说明:

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

    这告诉客户端不要以不确定的方式缓存响应…

    您可以手动将对象写入缓存:

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

    首先,您必须准备
    cachedResponse
    ,在这里您可以自己修改头。

    “默认情况下,连接的数据根据请求的缓存策略缓存”在文档中。是的,默认情况下。但是我没有使用默认的“NSURLRequestUseProtocolCachePolicy”,我使用的是“NSURLRequestReturnCacheDataElseLoad”,这意味着“使用缓存数据,忽略其年龄或到期日期”,这意味着我不能将NSURLCache与这样的http头一起使用?第二个问题呢?我应该使用什么缓存解决方案?