简体   繁体   English

NSURLCache cachedResponseForRequest不检索缓存数据

[英]NSURLCache cachedResponseForRequest doesn't retrieve cached data

I am trying to get the previously cached information from NSURLCache. 我试图从NSURLCache获取以前缓存的信息。 With this code: 使用此代码:

NSString *theCompleteURL = @"http://192.168.1.2:8080/api/endpoint.json";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:theCompleteURL]];

NSCachedURLResponse *response = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
if (response) {
    NSLog(@"Cached data found");
}
else {
    NSLog(@"Cached data not found");
}

But I always get nil as response for the "response" variable from the method cachedResponseForRequest. 但我总是从方法cachedResponseForRequest的“响应”变量的响应中得到nil。

I am sure about the data is inside the cache because I checked it in the Cache.db file of my application, getting this result from the cfurl_cache_response table: 我确信数据是在缓存中,因为我在我的应用程序的Cache.db文件中检查了它,从cfurl_cache_response表获取此结果:

sqlite> select * from cfurl_cache_response;
1|0|1875686237|0|http://192.168.1.2:8080/api/endpoint.json|2014-01-09 11:55:17|
sqlite> 

In the ApplicationDelegate the NSURLCache is configured as: 在ApplicationDelegate中,NSURLCache配置为:

NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:5 * 1024 * 1024
                                                  diskCapacity:40 * 1024 * 1024
                                                      diskPath:nil];

[NSURLCache setSharedURLCache:cache];

Any idea about what could be going on with the Cache? 关于缓存可能发生什么的任何想法?

The headers of my endpoints looks like: 我的端点的标题看起来像:

$ curl -I http://192.168.1.2:8080/api/endpoint.json
HTTP/1.1 200 OK
Content-Length: 1385
Expires:  Thu, 01 Dec 2020 16
00: 00 GMT
Content-Type: application/json;charset=utf-8
ETag:  "3e86-410-3596fbbc"
Cache-Control:  max-age=3600
Connection: keep-alive
Server: thin 1.5.1 codename Straight Razor
$ 

This could be explained by the headers associated with the response, specifically the Cache-control and Etag fields. 这可以通过与响应关联的标头来解释,特别是Cache-controlEtag字段。 See also here : 另请参见这里

13.1.3 Cache-control Mechanisms 13.1.3缓存控制机制

The basic cache mechanisms in HTTP/1.1 (server-specified expiration times and validators) are implicit directives to caches. HTTP / 1.1中的基本缓存机制(服务器指定的到期时间和验证程序)是缓存的隐式指令。 In some cases, a server or client might need to provide explicit directives to the HTTP caches. 在某些情况下,服务器或客户端可能需要向HTTP缓存提供显式指令。 We use the Cache-Control header for this purpose. 为此,我们使用Cache-Control标头。

The Cache-Control header allows a client or server to transmit a variety of directives in either requests or responses. Cache-Control标头允许客户端或服务器在请求或响应中传输各种指令。 These directives typically override the default caching algorithms. 这些指令通常会覆盖默认的缓存算法。 As a general rule, if there is any apparent conflict between header values, the most restrictive interpretation is applied (that is, the one that is most likely to preserve semantic transparency). 作为一般规则,如果标头值之间存在任何明显的冲突,则应用最严格的解释(即,最有可能保留语义透明性的解释)。

13.3.2 Entity Tag Cache Validators 13.3.2实体标记缓存验证器

The ETag response-header field value, an entity tag, provides for an "opaque" cache validator. ETag响应头字段值(实体标签)提供“不透明”缓存验证器。 This might allow more reliable validation in situations where it is inconvenient to store modification dates, where the one-second resolution of HTTP date values is not sufficient, or where the origin server wishes to avoid certain paradoxes that might arise from the use of modification dates. 这可能允许在存储修改日期不方便的情况下进行更可靠的验证,其中HTTP日期值的一秒分辨率不足,或者原始服务器希望避免使用修改日期可能产生的某些悖论。

You can find here the allowed values for Cache-control and here those for Etag. 你可以找到这里的Cache-Control和允许值这里那些Etag的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM