2016-08-02 91 views
0

嗨,我已阅读关于缓存策略,但仍不太清楚。我的目的是设置请求缓存3分钟。响应缓存3分钟。保留旧的缓存1天。我们应该使用什么实施?除了这些默认波纹管之外,是否有任何设置可以改变? (我使用AFNetworking 3进行请求和响应)。任何帮助非常感谢。由于我们应该使用什么缓存方法IOS应用程序

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. 

回答

1

你应该几乎无一例外地使用NSURLRequestUseProtocolCachePolicy,除非你试图杀死完全出于某种原因缓存。

缓存持续时间应由服务器设置为其响应的一部分(在Cache-Control标头中)。 iOS URL缓存服从该标题中指定的策略。

+0

嗨,感谢您的帮助。是否有任何自定义NSURLRequestUseProtocolCachePolicy更改属性的示例? –

+0

您的意思是将Cache-Control标题更改为缓存?我不知道是否有这样的例子,但是你可能通过实现'URLSession:dataTask:willCacheResponse:completionHandler:'委托方法并基于修改的**创建一个新的** NSCachedURLResponse **对象来实现它包含不同标题值的NSURLResponse **对象。 – dgatwood

+0

是的我的意思是定制使用自己的缓存实施,如果服务器不支持缓存头(设置缓存时间,大小,有效期......) –

相关问题