Which Cache Policy should be used?

275 Views Asked by At

My data is retrieve from a JSON API, and the data would changed per day. Currently, i'm using reloadIgnoringLocalCacheData now, which ignores the cache data and always retrieve data from the originating source.

My question is that could I just compare the cache with originating first and if they are not equal, then retrieve the data from server, otherwise use cache data. or maybe set an expired time for cache, how to implement that?

private var session: URLSession = {
        let config = URLSessionConfiguration.default
        config.urlCache = URLCache.shared
        config.waitsForConnectivity = true
        config.requestCachePolicy = .reloadIgnoringLocalCacheData
        return URLSession(configuration: config, delegate: nil, delegateQueue: nil)
    }()
    public enum CachePolicy : UInt {


        case useProtocolCachePolicy


        case reloadIgnoringLocalCacheData

        case reloadIgnoringLocalAndRemoteCacheData

        public static var reloadIgnoringCacheData: NSURLRequest.CachePolicy { get }


        case returnCacheDataElseLoad

        case returnCacheDataDontLoad


        case reloadRevalidatingCacheData
    }
0

There are 0 best solutions below