出于某种原因,我无法利用alamofire提供的缓存机制,并且我被迫清除缓存,因此我可以通过alamofire解析新的json内容。
我检查了控制标题和etag,我打印出响应和请求,但我发现了一些奇怪的东西。检查最大年龄。我的网站使用HTTP:
request <NSMutableURLRequest: 0x7a12b400> { URL: http://www.example.com/wp-json/posts }
response Optional(<NSHTTPURLResponse: 0x7a12fd30> { URL: http://www.example.com/wp-json/posts } { status code: 200, headers {
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Type" = "application/json; charset=UTF-8";
Date = "Sat, 26 Sep 2015 18:52:14 GMT";
"Keep-Alive" = "timeout=5, max=100";
"Last-Modified" = "Sat, 26 Sep 2015 17:43:31 GMT";
Link = "</wp-json/posts?page=2>; rel=\"next\",
Server = Apache;
**"Strict-Transport-Security" = "max-age=63072000; includeSubDomains";**
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding,User-Agent";
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-Pingback" = "http://www.example.com/xmlrpc.php";
"X-Powered-By" = "PHP/5.4.45";
"X-WP-Total" = 11;
"X-WP-TotalPages" = 2;
} })
如何更改max-age并使用reinvalidate控件标头,以便alamofire可以知道服务器上有新内容并尝试刷新缓存而不是加载旧数据?
感谢。
答案 0 :(得分:1)
您无法更改响应本身,但您可以更改您发出的第二个URL请求,以专门忽略缓存数据。
let URLRequest = NSMutableURLRequest(URL: NSURL(string: "https://httpbin.org/get")!)
URLRequest.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
Alamofire.request(URLRequest)
.responseJSON { request, response, result in
print(request)
print(response)
print(result)
}