Abhijit Sarkar
Abhijit Sarkar

Reputation: 24548

How can a HTTP client request the server for latest data/to refresh cache?

We're designing a REST service with server-side caching. We'd like to provide an option to the client to specifically ask for latest data even if the cached data has not expired. I'm looking into the HTTP 1.1 spec to see if there exists a standard way to do this, and the Cache Revalidation and Reload Controls appears to fit my need.

Questions:

Upvotes: 2

Views: 2330

Answers (2)

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24548

According to section-5.2.1.4, it appears that no-cache request directive best fits my need.

The "no-cache" request directive indicates that a cache MUST NOT use a stored response to satisfy the request without successful validation on the origin server.

Nothing is said about subsequent requests, which is exactly what I want. There is also a no-cache response directive in section-5.2.2.2, but that also applies to subsequent requests.

Upvotes: 2

Robert Bräutigam
Robert Bräutigam

Reputation: 7744

If your client wants a completely fresh representation of a resource, it may specify max-age=0 to do that. That is actually the intent to receive a response no older than 0 seconds.

All other mechanisms you mentioned (If-Modified-Since, ETag, If-Match, etc.) are all working with caches to make sure the resource is in some state. They work only, if you definitely know you have a valid state of the resource. You can think of it as optimistic locking. You can make conditional requests for when the resource did, or did not change. However you have to know whether you are expecting a change or not.

You could potentially misuse the If-Modified-Since as you say, but max-age communicates your intent better.

Also note, by design there may be multiple caches along the way, not just your server side cache. Most often the client caches also, and there may be other transparent caches on the way.

Upvotes: 2

Related Questions