我有缓存控制限制[private; max-age=15]
在我的服务器响应中设置为 15 秒。但即使在 15 秒后,响应也会从缓存中加载。我正在使用 swift3
& Alamofire 4.0
。
var request = URLRequest(url: URL(string: urlStr)!)
request.timeoutInterval = 30
request.httpMethod = "GET"
request.addValue("private", forHTTPHeaderField: "Cache-Control")
request.cachePolicy = .returnCacheDataElseLoad
request.addValue("token", forHTTPHeaderField: "auth_key")
Alamofire.request(request).responseObject(completionHandler: { (response: Response) in {
print(response)
}
最佳答案
您应该更改缓存策略。 Apple 的文档中写道,
case returnCacheDataElseLoad
Specifies that the existing cached data should be used to satisfy the request, regardless of its age or expiration date. If there is no existing data in the cache corresponding the request, the data is loaded from the originating source.
来源:https://developer.apple.com/reference/foundation/nsurlrequest.cachepolicy
查看列表,找出最适合您的政策。我建议将其设置为默认的 useProtocolCachePolicy
。
关于ios - 即使超过缓存控制期限限制,也会从缓存中加载值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43999726/