Giter Site home page Giter Site logo

alamofireurlcache's Introduction

AlamofireURLCache

Build Status Carthage Compatible Weibo

Alamofire network library URLCache-based cache extension

Features

  • Cache data and refresh
  • Ignore server config
  • Clear cache

Installation

Carthage

To integrate Alamofire into your Xcode project using Carthage, specify it in your Cartfile:

Swift 4 or Swift 5 (Alamofire version is 4.x)

github "kenshincui/AlamofireURLCache"

Swift 3 (Alamofire version is 3.x)

github "kenshincui/AlamofireURLCache" == 0.1

Run carthage update to build the framework and drag the built AlamofireURLCache.framework into your Xcode project.

Manually

If you prefer not to use any of the aforementioned dependency managers, you can integrate AlamofireURLCache into your project manually(Only need to copy AlamofireURLCache.swift to you project).

Usage

Cache and refresh

You can use the cache() method to save cache for this request, and set the request with the refreshCache parameter to re-initiate the request to refresh the cache data.

  • Simply cache data
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)
  • Refresh cache
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)

Ignore server-side cache configuration

By default, if the server is configured with cache headers, the server-side configuration is used, but you can use the custom cache age and ignore this configuration by setting the ignoreServer parameter。

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)

Clear cache

Sometimes you need to clean the cache manually rather than refresh the cache data, then you can use AlamofireURLCache cache cache API. But for network requests error, serialization error, etc. we recommend the use of autoClearCache parameters Automatically ignores the wrong cache data.

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)

When using AlamofireURLCache, we recommend that you add the autoClearCache parameter in any case.

License

AlamofireURLCache is released under the MIT license. See LICENSE for details.

alamofireurlcache's People

Contributors

kenshincui avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.