I would like to cache the data I am receiving from my API requests.
More precisely, I would like a 5 - 10 minutes maximum lifetime for my cache data.
Indeed, I am working on a weather app and I don't need database, but just to be able to reuse data from previous requests.
For example :
- User requests forecast data for New York
- User requests forecast data for London
Now if user requests New York again, I would like to reuse data from previous request. Weather data are updated every 15 minutes on the backend side (that's why 5 minutes cache is enough, navigation in app for a weather app doesn't last that much).
Here is what I have currently :
let configuration = URLSessionConfiguration.default
configuration.requestCachePolicy = .returnCacheDataElseLoad
configuration.timeoutIntervalForRequest = TimeInterval(60)
configuration.timeoutIntervalForResource = TimeInterval(60)
let session = URLSession(configuration: configuration)
The problem here is, if I request weather data for New York for example, and add the city in my favorite city. And then, if I never launch my App for 2 days, I would load the 2 days old cached data.
I would like to configure the expiration time of my URLSessionConfiguration.
NB: All my model objects come from Codable protocol and WebServices.