I am implementing an application that makes many network calls for rest-api, which we also control. Recently, we decided to introduce caching headers on the server side to save some valuable network and server time. Since we do not know in advance how long the data will be valid, we do not send Cache-control: max-age or Expires headers, all we do is send the Last-Modified header with the E-tag , so we always get to the server but the answers are pretty fast in most cases with 304 . At first, everything seemed to work fine, with many requests being cached. However, I am experiencing some random data errors in the application due to caching.
For some reason, I can’t understand, in some cases, requests are locally cached and used as “updated” data without getting to the server when they really aren’t. The problem persists until some time has passed. Then everything returns to the server again, just as it behaves with the cache-control header, but without it !. So my question is:
How can NSURLCache along with NSURLConnection decide that a particular request does not need to enter the network when the original request did not come with the Cache-control: max-age or Expires headers? Has anyone experienced similar effects? And how can I solve this problem without deleting the entire cache?
Additional Information:
- I use AFNetworking , but it relies on
NSURLConnection , so I don’t think it is changing anything. - The cache used is the default
[NSURLCache sharedURLCache] instance [NSURLCache sharedURLCache] This is a GET request, and when I check the headers from the cached response, this is what I get:
po [response allHeaderFields]
"Access-Control-Allow-Headers" = "Content-Type"; "Access-Control-Allow-Methods" = "GET, POST, DELETE, PUT"; "Access-Control-Allow-Origin" = "*"; Connection = "keep-alive"; "Content-Encoding" = gzip; "Content-Length" = 522; "Content-Type" = "application/json"; Date = "Mon, 02 Sep 2013 08:00:38 GMT"; Etag = "\"044ad6e73ccd45b37adbe1b766e6cd50c\""; "Last-Modified" = "Sat, 31 Aug 2013 10:36:06 GMT"; Server = "nginx/1.2.1"; "Set-Cookie" = "JSESSIONID=893A59B6FEFA51566023C14E3B50EE1E; Path=/rest-api/; HttpOnly";
I cannot predict or reproduce when an error occurs, so decisions that rely on deleting the cache are not an option.
- I am using iOS5 +
ios caching nsurlconnection afnetworking nsurlcache
Angel García Olloqui
source share