AlamofireURLCache 0.4.1

AlamofireURLCache 0.4.1

Jinkey 维护。



AlamofireURLCache

AlamofireURLCache for CocoaPods

这是不支持 CocoaPods (https://cocoapods.org.cn/) 的 kenshincui/AlamofireURLCache 的镜像。

您可以按以下方式安装:

pod 'AlamofireURLCache'

联系我

QQ 群 706964206

使用方法

缓存和刷新

您可以使用 cache() 方法为此请求保存缓存,并将带有 refreshCache 参数的请求设置为重新发起请求以刷新缓存数据。

  • 简单缓存数据
Alamofire.request("https://myapi.applinzi.com/url-cache/no-cache.php").responseJSON(completionHandler: { response in
    if response.value != nil {
        self.textView.text = (response.value as! [String:Any]).debugDescription
    } else {
        self.textView.text = "Error!"
    }
    
}).cache(maxAge: 10)
  • 刷新缓存
Alamofire.request("https://myapi.applinzi.com/url-cache/no-cache.php",refreshCache:true).responseJSON(completionHandler: { response in
    if response.value != nil {
        self.textView.text = (response.value as! [String:Any]).debugDescription
    } else {
        self.textView.text = "Error!"
    }
    
}).cache(maxAge: 10)

忽略服务器端缓存配置

默认情况下,如果服务器配置了缓存头,则使用服务器端配置,但您可以设置ignoreServer参数以使用自定义缓存年龄并忽略此配置。

Alamofire.request("https://myapi.applinzi.com/url-cache/default-cache.php",refreshCache:false).responseJSON(completionHandler: { response in
    if response.value != nil {
        self.textView.text = (response.value as! [String:Any]).debugDescription
    } else {
        self.textView.text = "Error!"
    }
    
}).cache(maxAge: 10,isPrivate: false,ignoreServer: true)

清除缓存

有时您需要手动清理缓存而不是刷新缓存数据,那么您可以使用AlamofireURLCache的cache API。但对于网络请求错误、序列化错误等情况,我们建议使用autoClearCache参数自动忽略错误缓存数据。

Alamofire.clearCache(dataRequest: dataRequest) // clear cache by DataRequest
Alamofire.clearCache(request: urlRequest) // clear cache by URLRequest

// ignore data cache when request error
Alamofire.request("https://myapi.applinzi.com/url-cache/no-cache.php",refreshCache:false).responseJSON(completionHandler: { response in
    if response.value != nil {
        self.textView.text = (response.value as! [String:Any]).debugDescription
    } else {
        self.textView.text = "Error!"
    }
    
},autoClearCache:true).cache(maxAge: 10)

当使用AlamofireURLCache时,我们建议在任何情况下都添加autoClearCache参数。