简体   繁体   中英

NSURLCache cachedResponseForRequest doesn't retrieve cached data

I am trying to get the previously cached information from 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.

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:

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:

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. See also here :

13.1.3 Cache-control Mechanisms

The basic cache mechanisms in HTTP/1.1 (server-specified expiration times and validators) are implicit directives to caches. In some cases, a server or client might need to provide explicit directives to the HTTP caches. We use the Cache-Control header for this purpose.

The Cache-Control header allows a client or server to transmit a variety of directives in either requests or responses. 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

The ETag response-header field value, an entity tag, provides for an "opaque" cache validator. 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.

You can find here the allowed values for Cache-control and here those for Etag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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