簡體   English   中英

Cache-Control:max-age不會使AFNetworking緩存響應

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM