I am trying to cache a JSON response (https://api.github.com/search/repositories) with the help of Alamofire's ResponseCacher with a expiry of 2 hours in the client. In one of the posts in SO, Jon of Alamofire had suggested to modify the CachedURLResponse in the ResponseCacher. The response does not have a Cache-Control or any cache headers. So I am manually adding the cache-control header in the response. I have also put together the session configuration code from my understanding, however I can't seem to get the cache working with the overridden expiry. The session definition is as follows:
let sessionConfiguration = URLSessionConfiguration.af.default
sessionConfiguration.requestCachePolicy = .useProtocolCachePolicy
sessionConfiguration.urlCache = URLCache(memoryCapacity: 20 * 1024 * 1024, diskCapacity: 100 * 1024 * 1024)
let responseCacher = ResponseCacher(behavior: .modify { _, response in
return CachedURLResponse(
response: response.response,
data: response.data,
userInfo: userInfo,
storagePolicy: .allowed)
.response(with: 7200)
})
let aSession = Session(configuration: sessionConfiguration, cachedResponseHandler: responseCacher)
where the response modifier is defined as follows
extension CachedURLResponse {
func response(with expirationDuration: Int) -> CachedURLResponse {
var cachedResponse = self
if let httpResponse = cachedResponse.response as? HTTPURLResponse, var headers = httpResponse.allHeaderFields as? [String : String], let url = httpResponse.url{
headers["Cache-Control"] = "private, max-age=\(expirationDuration)"
headers.removeValue(forKey: "Expires")
headers.removeValue(forKey: "s-maxage")
if let newResponse = HTTPURLResponse(url: url, statusCode: httpResponse.statusCode, httpVersion: "HTTP/1.1", headerFields: headers) {
cachedResponse = CachedURLResponse(response: newResponse, data: cachedResponse.data, userInfo: headers, storagePolicy: cachedResponse.storagePolicy)
}
}
return cachedResponse
}
}
If I set the requestCachePolicy to .useProtocolCachePolicy, when network is offline, I don't get any cached response. If I set the requestCachePolicy to .returnCacheDataElseLoad the cache never expires. I need to cache my response, but also have it expire after 2 hours and return the cached response when network is offline.
I am beginner when it comes to caching so there are places I have made some leaps without completely understanding how it works. Parts of the code are taken from SO.