【问题标题】:Value getting loaded from cache even after exceeding the cache control age limit即使超过缓存控制年龄限制,值也会从缓存中加载
【发布时间】:2017-05-16 11:05:22
【问题描述】:

我在服务器响应中将缓存控制限制 [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)
        }

【问题讨论】:

    标签: ios iphone swift swift3 alamofire


    【解决方案1】:

    您应该更改缓存策略。 苹果的文档中写道,

    case returnCacheDataElseLoad 指定现有的缓存数据 应用于满足请求,无论其年龄或 到期日期。如果缓存中没有现有数据 对应于请求,数据从原始加载 来源。

    来源:https://developer.apple.com/reference/foundation/nsurlrequest.cachepolicy

    查看列表,找出最适合您的政策。我建议将其设置为默认的useProtocolCachePolicy

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      相关资源
      最近更新 更多