我们应该使用什么缓存方法 IOS app

What Cache method should we use IOS app

您好,我已经阅读了有关缓存策略的内容,但仍然不是很清楚。我的目的是设置请求缓存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.

你应该几乎总是使用 NSURLRequestUseProtocolCachePolicy 除非你出于某种原因试图完全杀死缓存。

缓存持续时间应由服务器设置为响应的一部分(在 Cache-Control header 中)。 iOS URL 缓存服从 header.

中指定的策略