如何设置NSURLRequest缓存过期? [英] How to set NSURLRequest cache expiration?
问题描述
我正在使用AFNetworking,并且需要在一个响应中将数据缓存几分钟.因此,我在应用程序委托中设置了NSUrlCache,然后在我的请求中对其进行了设置:
I'm using AFNetworking and need to cache data in one response for a several minutes. So I set NSUrlCache in app delegate and then in my request setting up it:
NSMutableURLRequest *request = //obtain request;
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
然后如何设置到期日期:如果数据加载时间超过n分钟,请询问服务器而不是磁盘的响应?
How then set expiration date: if the data was loaded more than n minutes ago, ask response from server and not from disk?
假设服务器不支持缓存,我需要用代码进行管理.
Assume that server doesn't support caching, I need to manage it in code.
推荐答案
因此,我找到了解决方案.
So, I found the solution.
想法是使用connection:willCacheResponse:
方法.在缓存响应之前,它将被执行,我们可以在其中更改响应并返回new,或者返回nil,并且响应不会被缓存.当我使用AFNetworking时,有一个很好的操作方法:
The idea is to use connection:willCacheResponse:
method. Before cache the response it will be executed and there we can change response and return new, or return nil and the response will not be cached. As I use AFNetworking, there is a nice method in operation:
- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block;
添加代码:
[operation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
if([connection currentRequest].cachePolicy == NSURLRequestUseProtocolCachePolicy) {
cachedResponse = [cachedResponse responseWithExpirationDuration:60];
}
return cachedResponse;
}];
其中responseWithExpirationDuration
来自类别:
@interface NSCachedURLResponse (Expiration)
-(NSCachedURLResponse*)responseWithExpirationDuration:(int)duration;
@end
@implementation NSCachedURLResponse (Expiration)
-(NSCachedURLResponse*)responseWithExpirationDuration:(int)duration {
NSCachedURLResponse* cachedResponse = self;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)[cachedResponse response];
NSDictionary *headers = [httpResponse allHeaderFields];
NSMutableDictionary* newHeaders = [headers mutableCopy];
newHeaders[@"Cache-Control"] = [NSString stringWithFormat:@"max-age=%i", duration];
[newHeaders removeObjectForKey:@"Expires"];
[newHeaders removeObjectForKey:@"s-maxage"];
NSHTTPURLResponse* newResponse = [[NSHTTPURLResponse alloc] initWithURL:httpResponse.URL
statusCode:httpResponse.statusCode
HTTPVersion:@"HTTP/1.1"
headerFields:newHeaders];
cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:newResponse
data:[cachedResponse.data mutableCopy]
userInfo:newHeaders
storagePolicy:cachedResponse.storagePolicy];
return cachedResponse;
}
@end
因此,我们根据 http/1.1 在http标头中设置以秒为单位的到期时间 为此,我们需要设置以下标头之一: 过期,缓存控制:s-maxage或max-age 然后创建新的缓存响应,因为属性是只读的,并返回新对象.
So, we set expiration in seconds in http header according to http/1.1 For that we need one of headers to be set up: Expires, Cache-Control: s-maxage or max-age Then create new cache response, because the properties is read only, and return new object.
这篇关于如何设置NSURLRequest缓存过期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!