Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the default cache expiration time for NSURLRequests?

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.

like image 853
henrik Avatar asked Apr 13 '12 09:04

henrik


People also ask

What is cache expire time?

It means that a cache should not return the response-entity after that time, unless it is first validated against the origin server. The Expires entity-header field uses the following syntax (where HTTP-date is an absolute date and time that must be in the RFC1123 date format): Expires = "Expires" ":" HTTP-date.

How do I set my cache to expire?

One of the way to Set cache expiration is by using . htaccess file. Below code will set expiration for it's respective file type, e.g. for CSS files expiration will be 14 days.

What is cache policy in Swift?

Cache Policy #4: returnCacheDataDontLoad Apple describes the cache policy as: “If there is no existing data in the cache corresponding to a URL load request, no attempt is made to load the data from the originating source, and the load is considered to have failed.


1 Answers

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.

like image 64
N_A Avatar answered Sep 23 '22 14:09

N_A