What's the default cache expiration time for NSURLRequests?

henrik picture henrik · Apr 13, 2012 · Viewed 7.6k times · Source

I'm using a NSURLRequest to check for available data updates. Today I noticed, that NSURLRequest caches the request by default. Even after several hours no new request was sent to the server. Now I'm wondering what the default behavior of this cache is. When will the cached data be stale and a new request send to the server? The data is a static file and the server does not send explicit cache control headers:

HTTP/1.1 200 OK
Date: Fri, 13 Apr 2012 08:51:13 GMT
Server: Apache
Last-Modified: Thu, 12 Apr 2012 14:02:17 GMT
ETag: "2852a-64-4bd7bcdba2c40"
Accept-Ranges: bytes
Content-Length: 100
Vary: Accept-Encoding,User-Agent
Content-Type: text/plain

P.S.: The new version of my app sets an explicit caching policy, so that this isn't a problem anymore, but I'm curious what the default behavior is.

Answer

N_A picture N_A · Apr 16, 2012

Note: here specifies how this should work in detail

  1. If there is no cache then fetch the data.

  2. If there is a cache, then check the loading scheme

    a. if re-validation is specified, check the source for changes

    b. if re-validation is not specified then fetch from the local cache as per 3)

  3. If re-validation is not specified the local cache is checked to see if it is recent enough.

    a. if the cache is not stale then it pulls the data from the cache

    b. if the data is stale then it re-validates from the source

From here:

The default cache policy for an NSURLRequest instance is NSURLRequestUseProtocolCachePolicy. The NSURLRequestUseProtocolCachePolicy behavior is protocol specific and is defined as being the best conforming policy for the protocol

From here:

If an NSCachedURLResponse does not exist for the request, then the data is fetched from the originating source. If there is a cached response for the request, the URL loading system checks the response to determine if it specifies that the contents must be revalidated. If the contents must be revalidated a connection is made to the originating source to see if it has changed. If it has not changed, then the response is returned from the local cache. If it has changed, the data is fetched from the originating source.

If the cached response doesn’t specify that the contents must be revalidated, the maximum age or expiration specified in the response is examined. If the cached response is recent enough, then the response is returned from the local cache. If the response is determined to be stale, the originating source is checked for newer data. If newer data is available, the data is fetched from the originating source, otherwise it is returned from the cache.

Other options listed here.