您好我读过有关缓存策略但仍然不太清楚。我的目的是将请求缓存设置为3分钟。响应缓存3分钟。将旧缓存保留1天。我们应该使用什么实现?除了那些默认的波纹管之外,我们可以更改任何设置吗? (我使用AFNetworking 3进行请求和响应)。任何帮助都非常感激。感谢
NSURLRequestCachePolicy
NSURLRequest has a cachePolicy property, which specifies the caching behavior of the request according to the following constants:
NSURLRequestUseProtocolCachePolicy: Caching logic defined in the protocol implementation is used for a particular URL load request. This is the default policy.
NSURLRequestReloadIgnoringLocalCacheData: Data should be loaded from the originating source. No existing cache data should be used.
NSURLRequestReloadIgnoringLocalAndRemoteCacheData: Not only should the local cache data be ignored, but proxies and other intermediates should be instructed to disregard their caches so far as the protocol allows.
NSURLRequestReturnCacheDataElseLoad: Existing cached data should be used, regardless of its age or expiration date. If there is no existing data in the cache corresponding to the request, the data is loaded from the originating source.
NSURLRequestReturnCacheDataDontLoad: Existing cache data should be used, regardless of its age or expiration date. If there is no existing data in the cache corresponding to the request, no attempt is made to load the data from the originating source, and the load is considered to have failed, (i.e. “offline” mode).
NSURLRequestReloadRevalidatingCacheData: Existing cache data may be used provided the origin source confirms its validity, otherwise the URL is loaded from the origin source.
答案 0 :(得分:1)
你应该几乎总是使用NSURLRequestUseProtocolCachePolicy,除非你因某种原因试图完全杀死缓存。
缓存持续时间应由服务器设置为其响应的一部分(在Cache-Control
标头中)。 iOS URL缓存遵循该标头中指定的策略。