ios - 缓存控制 : max-age does not make AFNetworking cache the response

标签 ios objective-c caching afnetworking afnetworking-2

在服务器端,我正在设置 Cache-Control:max-age=14400。

在我的 iOS 客户端中,我正在设置 AFHTTPSessionManager 的共享实例,如下所示:

+ (TKClient *)sharedInstance {
static TKClient *instance = nil;
static dispatch_once_t token;

dispatch_once(&token, ^ {
    instance = [[TKClient alloc] initWithBaseURL:[NSURL URLWithString:kTKBaseUrl]];
    [instance setRequestSerializer:[AFJSONRequestSerializer serializer]];
});

return instance;

然后像这样调用我的 API:

- (void)listingsWithParams:(NSDictionary *)params completion:(void (^)(NSMutableArray *listings, NSError *error))completion {
[self GET:@"api/listings" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
    // success here
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    // error here
}];

我已验证此列表调用的缓存控制 header 已从服务器端正确返回。但是,调用每次都会转到服务器,而且显然没有被缓存。知道问题出在哪里吗?

我是否需要在 iOS 端配置其他东西,或者以某种方式更改 Cache-Control header ?谢谢!

最佳答案

尝试添加这个:

[connectionManager.requestSerializer setCachePolicy:NSURLRequestReturnCacheDataElseLoad];

编辑 如果您已完成所有操作,请再次检查 header ,也许您在 API 上遗漏了一些东西! 也许你应该使用 "Cache-Control"= "public, max-age=14400";public 关键字添加到 Cache-Control header ?

public 表示响应可以被任何缓存缓存,即使它通常是不可缓存的或只能在非共享缓存中缓存。

关于ios - 缓存控制 : max-age does not make AFNetworking cache the response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31691910/

相关文章:

objective-c - 我应该在 Objective C 中哪里初始化变量?

用户当前位置更新时iOS mapKit刷新方向

asp.net - 缓存ASP.NET HTTP处理程序服务器和客户端的响应

java - @Cacheable 不拦截方法,缓存永远为空

ios - 为什么我的 json 响应不符合我的结构?

ios - Firebase 和 Google Signin 给出重复符号错误

ios - 需要有关使用核心位置计算距离的帮助

ios - 在 Objective-C 中使用带或不带后缀 .f 的 CGFloat 是否存在性能差异

html - 缓存 list 在联机时弄乱了我的应用程序

ios - Watchkit 应用程序的退出由 Watchkit 操作系统本身处理,我不需要清除或重置屏幕?