![](/img/trans.png)
[英]URLResponse cached although response's cache-control header is set to no-cache
[英]NSURLCache doesn't work (with Cache-Control = no-cache)
我在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;
從Apple文檔中 :
NSURLRequestReturnCacheDataElseLoad高速緩存策略使URL加載系統使用高速緩存的數據,而忽略其使用期限或到期日期,並且僅當沒有高速緩存的版本時才從原始源加載數據。
我的問題是:
它應該在響應頭中使用“ Cache-Control” =“ no-cache,no-store,max-age = 0,must-revalidate”來工作(我的意思是緩存服務器響應)嗎?
如果沒有,我應該使用什么解決方案來緩存服務器答案? (我無法在服務器端進行任何更改)
在Cache-Control標頭中清楚地闡明了這一點:
"Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate";
這告訴客戶端不要以不確定的方式緩存響應...
您可以手動將對象寫入緩存:
NSURLCache *sharedCache = [NSURLCache sharedURLCache];
[sharedCache storeCachedResponse:cachedResponse forRequest:request];
首先,您必須准備cachedResponse
,在這里您可以自己修改標頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.