ios - 即使超过缓存控制期限限制,也会从缓存中加载值

标签 ios iphone swift swift3 alamofire

我有缓存控制限制[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/

相关文章:

objective-c - 在我的 UITableView 上放置渐变以显示单元格

ios - 谷歌地图全屏显示。如何将其填充到半屏?

ios - 如何对特定于语言环境的本地通知进行单元测试

iphone - 添加 subview 后未收到touchesEnded/Moved/Cancelled

ios - 不允许在释放时尝试加载 View Controller 的 View ,这可能会导致未定义的行为

从脚本生成构建时,SwiftSupport 文件夹未包含在 IPA 中

arrays - Swift:在数组中查找值并返回特定键

ios - UITapGestureRecognizer 不适用于 .began 状态

ios - UICollectionView 应用程序在 iPhone 上显示大量图像时崩溃

ios - 快速下载 url 并填充 uiimageview