evermeer / EVURLCache

a NSURLCache subclass for handling all web requests that use NSURLRequest
Other
297 stars 49 forks source link

Add Cache-Control expiration if server header is not set #48

Closed alexookah closed 6 years ago

alexookah commented 7 years ago

Hi,

i am trying to modify the header of the response. and i was looking inside the library maybe there was a way to add an option to Modify or add a Cache-Control header?

if !EVURLCache.ADD_CACHE_CONTROL {
    if let httpResponse = cachedResponse.response as? HTTPURLResponse {
        var headers = httpResponse.allHeaderFields as? [String : String]
        headers!["Cache-Control"] = "max-age=\(EVURLCache.CACHE_EXPIRATION_SECONDS)"
    }
}
evermeer commented 7 years ago

EVURLCache does take into account the header variables Cache-Control and Pragma when caching a response. So if you want to control is server side, you could set one of these. You do have to make sure that you did not set EVURLCache.IGNORE_CACHE_CONTROL to true. Otherwise the headers will be ignored.

You could also arrange saving the response inside your app. If you want to filter out specific requests from being cashed, you could arrange that in your app by using a filter like this:

        EVURLCache.filter { request in
            if request.url?.host == "githubbadge.appspot.com" {
                return false
            }
            return true
        }
alexookah commented 7 years ago

I tried many ways to modify the response headers but it seems EVURL was not respecting that.

i ended up using. the EVURLCache.MAX_AGE = "60". which seems to do my job.

i only found that when the cache is not expired and i do reloadIgnoringCacheData. should the response be cached if the content is not the same with the cached content?

evermeer commented 7 years ago

In the storeCachedResponse there is a mechanism that even if it should not cache but the file is already in the preCache, it will still save it so that you will have the latest version if the next request will use another cache policy

in the log you will see a message that starts with: CACHE file in PreCache folder, overriding

alexookah commented 7 years ago

Great thanks. Is it ok if i use EVURLCache.MAX_AGE = "60". rather than configuring the Cache-Control from the API?

evermeer commented 7 years ago

Sure you can. By default EVURLCache will use the Access-Control-Max-Age header field and when that's empty it will use the MAX_AGE like this:

let maxAge: String = request.value(forHTTPHeaderField: "Access-Control-Max-Age") ?? EVURLCache.MAX_AGE

alexookah commented 7 years ago

is there a difference between Access-Control-Max-Age and Cache-Control:max-age in EVURLCache?

evermeer commented 7 years ago

EVURLCache only uses Access-Control-Max-Age For the Cache-Control parameter it will only use the no-store and no-cache values. Do you think it should also consider the max-age parameter? But then you could have situations where these 2 are different. So what to do then?

For more info about these 2 can be found at: https://stackoverflow.com/questions/40731465/access-control-max-age-vs-cache-control

alexookah commented 7 years ago

So i guess the Cache-Control:max-age doesnt make any difference for EVURLCache ? is it handled in Alamofire request ?

evermeer commented 7 years ago

I would be surprised if Alamofire did something with the Cache-Control:max-age header