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
andEtag
fields. See also here:You can find here the allowed values for Cache-control and here those for Etag.