gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:50:03 26 4
gpt4 key购买 nike

我在 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];

但它根本不起作用(它每次都请求服务器,甚至不尝试使用缓存)。

我检查了服务器响应中的 header ,这里是:

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

我的问题是:

  1. 它是否应该在响应 header 中使用“Cache-Control”=“no-cache,no-store,max-age=0,must-revalidate”工作(我的意思是缓存服务器响应)?

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

最佳答案

它在您的 Cache-Control header 中非常清楚地拼写出来:

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

明确告诉客户端不要缓存响应...

关于ios - NSURLCache 不起作用(使用 Cache-Control = no-cache),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20826769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com