gpt4 book ai didi

ios - NSURLCache - 参数不起作用的 GET 请求的磁盘缓存

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:11 33 4
gpt4 key购买 nike

我正在尝试使用 NSURLCache 将一些 HTTP 请求缓存到磁盘。在使用 AFNetworking 进行了一些测试但没有成功后,我使用 NSURLRequest 创建了一个简单的示例。

我像这样初始化 AppDelegate 上的缓存,将内存大小设置为 0,强制它始终转到磁盘:

NSUInteger sz = 1024*1024*20;
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:sz diskPath:nil];
[NSURLCache setSharedURLCache:cache];

我使用以下代码来存储响应:

NSURLCache *urlCache = [NSURLCache sharedURLCache];

NSString *urlString = @"http://localhost:8000/cities/api/cities/?lang=en";
NSHTTPURLResponse *response = nil;
NSError *error = nil;

NSURL *finalUrl = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalUrl];
request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;

NSData *data = nil;

data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data userInfo:nil storagePolicy:NSURLCacheStorageAllowed];
[urlCache storeCachedResponse:cachedResponse forRequest:request];

我的请求有 HTTP header 缓存控制,如:

response['cache-control'] = 'max-age=36000, public'

我看到响应缓存在文件 Cache.db 中。

然后,在下一次执行甚至同一次执行中,我使用以下代码尝试从缓存中获取响应:

NSCachedURLResponse *cachedResponse = [urlCache cachedResponseForRequest:request];
response = cachedResponse.response;
data = cachedResponse.data;

问题是 cachedResponse 始终为 null 当请求包含 GET 参数时。如果请求是“http://localhost:8000/cities/api/cities/”,则存储结果并稍后恢复。但是当它确实有 GET 参数时,某些东西会阻止方法 [NSURLCache cachedResponseForRequest:request] 找到请求。

鉴于此,我将 NSURLCache 子类化并像这样实现该方法:

- (id)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity diskPath:(NSString *)path {
self = [super initWithMemoryCapacity:memoryCapacity diskCapacity:diskCapacity diskPath:path];
if (self) {
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *dir = (NSString*)[docPaths objectAtIndex:0];
dir = [dir stringByAppendingString:@"/uk.co.airsource.TestURLCache/nsurlcache"];
NSString *path = [dir stringByAppendingFormat:@"/Cache.db"];
self.db = [[FMDatabase alloc] initWithPath:path];
[self.db open];
int o = 4;
}
return self;

- (NSCachedURLResponse *) cachedResponseForRequest:(NSURLRequest *)request {
NSCachedURLResponse *cachedURLResponse = [super cachedResponseForRequest:request];
if (cachedURLResponse == nil && [request.HTTPMethod isEqualToString:@"GET"]) {
//[db executeQuery:@"select * from test where a = ?", @"hi'", nil];
NSLog(@"%@", request.URL.absoluteString);
FMResultSet *cfurl_cache_response = [self.db executeQuery:@"select * from cfurl_cache_response where request_key = ? limit 1", request.URL.absoluteString, nil];
if ([cfurl_cache_response next]) {
id entry_ID = [cfurl_cache_response objectForColumnName:@"entry_ID"];
[cfurl_cache_response close];
if (entry_ID != [NSNull null]) {
FMResultSet *cfurl_cache_blob_data = [self.db executeQuery:@"select * from cfurl_cache_blob_data where entry_ID = ? limit 1", entry_ID, nil];
if ([cfurl_cache_blob_data next]) {
id response_object = [cfurl_cache_blob_data objectForColumnName:@"response_object"];
[cfurl_cache_blob_data close];
FMResultSet *cfurl_receiver_data = [self.db executeQuery:@"select * from cfurl_cache_receiver_data where entry_ID = ? limit 1", entry_ID, nil];
if ([cfurl_receiver_data next]) {
id receiver_data = [cfurl_receiver_data objectForColumnName:@"receiver_data"];
[cfurl_receiver_data close];
if (response_object != [NSNull null] && receiver_data != [NSNull null] && response_object && receiver_data) {
NSURLResponse *urlResponse = [[NSURLResponse alloc] initWithURL:request.URL MIMEType:[[request allHTTPHeaderFields] objectForKey:@"Accept"] expectedContentLength:[(NSData *)response_object length] textEncodingName:nil];
cachedURLResponse = [[NSCachedURLResponse alloc] initWithResponse:urlResponse data:receiver_data userInfo:nil storagePolicy:NSURLCacheStorageAllowed];
}
}
}
}
}
}
return cachedURLResponse;

在这种情况下,在带参数的 GET 请求下,它会跟随每一行,直到获得之前缓存的 NSURLResponse。

关于为什么当请求有 GET 参数时它不能正常工作有什么想法吗?

DB with the 2 requests stored

最佳答案

问题似乎是没有指定内存容量,如果你指定内存容量如下,

这里我指定内存容量为 1 MB 的大小

[[NSURLCache alloc] initWithMemoryCapacity:1*1024*1024 diskCapacity:sz diskPath:nil];

会用的,在我看来好像内存容量是需要的,容量再小又不能为空。然后您将能够提取存储的缓存,如下所示,

NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
response = cachedResponse.response;
data = cachedResponse.data;

关于ios - NSURLCache - 参数不起作用的 GET 请求的磁盘缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28460190/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com