![](/img/trans.png)
[英]Alamofire/NSURLSession caching with private Cache-Control and max-age
[英]Cache-Control: max-age does not make AFNetworking cache the response
在服务器端,我正在设置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标头? 谢谢!
尝试添加:
[connectionManager.requestSerializer setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
编辑再次检查标题如果您已经完成了所有操作,可能会遗漏API上的内容! 也许你应该使用"Cache-Control" = "public, max-age=14400";
将public
关键字添加到Cache-Control
标头?
public
表示响应可以由任何缓存缓存,即使它通常只能在非共享缓存中不可缓存或可缓存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.