ios – 绕过http响应头缓存控制:如何设置缓存过期?

前端之家收集整理的这篇文章主要介绍了ios – 绕过http响应头缓存控制:如何设置缓存过期?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
来自服务器的所有http响应都带有标题,通知我们的应用程序不缓存响应:
  1. Cache-Control: no-cache
  2. Pragma: no-cache
  3. Expires: 0

因此,如果您使用默认缓存策略“NSURLRequestUseProtocolCachePolicy”进行NSUrlRequests,则应用程序将始终从服务器加载数据.然而,我们需要缓存响应,明显的解决办法是将这些头设置为一段时间,例如(在后端),设置为10秒.但是我对解决方案感兴趣,如何绕过此策略并缓存每个请求10秒.

为此,您需要设置共享缓存.这可能在AppDelegate didFinishLaunchingWithOptions中完成:

  1. NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
  2. diskCapacity:20 * 1024 * 1024
  3. diskPath:nil];
  4. [NSURLCache setSharedURLCache:URLCache];

然后,我们需要嵌入我们的代码强制缓存一个响应.如果您使用AFHttpClient的实例,则可以通过覆盖下面的方法并将高速缓存手动存储到共享缓存中来完成:

  1. - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
  2. willCacheResponse:(NSCachedURLResponse *)cachedResponse {
  3.  
  4. NSMutableDictionary *mutableUserInfo = [[cachedResponse userInfo] mutableCopy];
  5. NSMutableData *mutableData = [[cachedResponse data] mutableCopy];
  6. NSURLCacheStoragePolicy storagePolicy = NSURLCacheStorageAllowedInMemoryOnly;
  7.  
  8. // ...
  9.  
  10. return [[NSCachedURLResponse alloc] initWithResponse:[cachedResponse response]
  11. data:mutableData
  12. userInfo:mutableUserInfo
  13. storagePolicy:storagePolicy];
  14. }

最后一件事是为请求设置cachePolicy.在我们的例子中,我们要为所有请求设置相同的缓存策略.所以再一次,如果你使用一个AFHttpClient的实例,那么可以通过覆盖下面的方法

  1. - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
  2.  
  3. NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
  4. request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
  5.  
  6. return request;
  7. }

到现在为止还挺好. “NSURLRequestReturnCacheDataElseLoad”使第一次执行请求,并从其他时间加载缓存的响应.问题是,目前还不清楚如何设置缓存到期时间,例如10秒.

解决方法

您可以实现一个自定义NSURLCache,它只返回尚未过期的缓存响应.

例:

  1. #import "CustomURLCache.h"
  2.  
  3. NSString * const EXPIRES_KEY = @"cache date";
  4. int const CACHE_EXPIRES = -10;
  5.  
  6. @implementation CustomURLCache
  7.  
  8. // static method for activating this custom cache
  9. +(void)activate {
  10. CustomURLCache *urlCache = [[CustomURLCache alloc] initWithMemoryCapacity:(2*1024*1024) diskCapacity:(2*1024*1024) diskPath:nil] ;
  11. [NSURLCache setSharedURLCache:urlCache];
  12. }
  13.  
  14. -(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
  15. NSCachedURLResponse * cachedResponse = [super cachedResponseForRequest:request];
  16. if (cachedResponse) {
  17. NSDate* cacheDate = [[cachedResponse userInfo] objectForKey:EXPIRES_KEY];
  18. if ([cacheDate timeIntervalSinceNow] < CACHE_EXPIRES) {
  19. [self removeCachedResponseForRequest:request];
  20. cachedResponse = nil;
  21. }
  22. }
  23.  
  24. return cachedResponse;
  25. }
  26.  
  27. - (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request {
  28. NSMutableDictionary *userInfo = cachedResponse.userInfo ? [cachedResponse.userInfo mutableCopy] : [NSMutableDictionary dictionary];
  29. [userInfo setObject:[NSDate date] forKey:EXPIRES_KEY];
  30. NSCachedURLResponse *newCachedResponse = [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response data:cachedResponse.data userInfo:userInfo storagePolicy:cachedResponse.storagePolicy];
  31.  
  32. [super storeCachedResponse:newCachedResponse forRequest:request];
  33. }
  34.  
  35. @end

如果这没有给你足够的控制权,那么我将使用如下的startLoading方法实现一个定制的NSURLProtocol,并将它与自定义缓存一起使用.

  1. - (void)startLoading
  2. {
  3. NSMutableURLRequest *newRequest = [self.request mutableCopy];
  4. [NSURLProtocol setProperty:@YES forKey:@"CacheSet" inRequest:newRequest];
  5.  
  6. NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:self.request];
  7. if (cachedResponse) {
  8. [self connection:nil didReceiveResponse:[cachedResponse response]];
  9. [self connection:nil didReceiveData:[cachedResponse data]];
  10. [self connectionDidFinishLoading:nil];
  11. } else {
  12. _connection = [NSURLConnection connectionWithRequest:newRequest delegate:self];
  13. }
  14. }

一些链接

> Useful info on NSURLCache
> Creating a custom NSURLProtocol

猜你在找的iOS相关文章