【问题标题】:Cache-Control: max-age does not make AFNetworking cache the responseCache-Control: max-age 不会让 AFNetworking 缓存响应
【发布时间】:2015-10-19 22:19:09
【问题描述】:

在服务器端,我设置了 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
}];

我已验证该列表调用的 Cache-Control 标头已从服务器端正确返回。但是,调用每次都转到服务器,并且显然没有被缓存。知道可能是什么问题吗?

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

【问题讨论】:

  • 还有一件事 - 如果我设置 instance.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;然后一切都被超级积极地缓存,即使我退出应用程序但我想确保东西只缓存一段时间。

标签: ios objective-c caching afnetworking afnetworking-2


【解决方案1】:

尝试添加这个:

[connectionManager.requestSerializer setCachePolicy:NSURLRequestReturnCacheDataElseLoad];

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

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

【讨论】:

  • @Ken 我没有看到你上面的评论很抱歉,但我正在努力提供帮助,我编辑了我的答案,希望对你有所帮助!
  • 我为这个假设道歉,这个问题对我来说非常棘手。我会试一试,谢谢!
猜你喜欢
  • 2021-04-15
  • 2020-08-11
  • 2018-03-14
  • 2018-11-24
  • 1970-01-01
  • 2019-07-09
  • 2018-10-06
  • 1970-01-01
  • 2012-06-30
相关资源
最近更新 更多