6

According to http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10 clients must invalidate the cache associated with a URL after a POST, PUT, or DELETE request.

Is it possible to instruct a web browser to invalidate the cache of an arbitrary URL, without making an HTTP request to it?

For example:

  1. PUT /companies/Nintendo creates a new company called "Nintendo"
  2. GET /companies lists all companies
  3. Every time I create a new company, I want to invalidate the cache associated with GET /companies. The browser doesn't do this automatically because the two operate on different URLs.

Is the Cache-Control mechanism inappropriate for this situation? Should I use no-cache along with ETag instead? What is the best-practice for this situation?

I know I can pass no-cache the next time I GET /companies but that requires the application to keep track URL invalidation instead of pushing the responsibility to the browser. Meaning, I want to invalidate the URL after step 1 as opposed to having to persist this information and applying it at step 2. Any ideas?

Gili
  • 86,244
  • 97
  • 390
  • 689

3 Answers3

2

Yes, you can (within the same domain). From this answer (slightly paraphrased):

In response to a PUT or POST request, if the Content-Location header URI is different from the request URI, then the cache for the Content-Location URI is invalidated.

So in your case, include a Content-Location: /companies header in response to your POST request. This will invalidate the browser's cached version of /companies.

Note that this does not work for GET requests.

Dragon
  • 173
  • 10
0

No, in HTTP/1.1 you may only invalidate a client's cache for a resource in a response to a request for that resource. It may be in response to a PUT, POST or DELETE rather than a GET (see RFC 7234, section 4.4 for details).

If you have a resource where you need clients to confirm that they have the latest version then no-cache and an entity tag is an ideal solution.

HTTP/2 allows for pushing a cache clear (Nine Things to Expect from HTTP/2 4. Cache Pushing).

Community
  • 1
  • 1
Joe
  • 29,416
  • 12
  • 68
  • 88
-1

In the link which you have given "the phrase "invalidate an entity" means that the cache will either remove all instances of that entity from its storage, or will mark these as "invalid" and in need of a mandatory revalidation before they can be returned in response to a subsequent request.". Now the question is where are the caches? I believe the Cache the article is talking about is the server cache.

I have worked on a project in VC++ where whenever a model changes the cache is updated. There is a programming logic implemention involved to achieve this. Your mentioned article rightly says "There is no way for the HTTP protocol to guarantee that all such cache entries are marked invalid" HTTP Protocol cannot invalidate cache on its own.

In our project example we used publish subscribe mechanism. Wheneven an Object of class A is updated/inserted it is published to a bus. The controllers register to listen to objects on the Bus. Suppose A Controller is interested in Object A changes, it will not be called back whenever Object Type B is changed and published. When Object Type A indeed is changed and published then Controller A Listener function updates the Cache with latest changes of Object A. The subsequent request of GET /companies will get the latest from the cache. Now there is a time gap between changing the object A and the Cache being refreshed with the latest changes. To avoid something wrong happening in this time gap Object is marked dirty before the Object A Changes. So a request coming inbetween of these times will wait for dirty flag being cleared.

There is also a browser cache. I remember ETAGS are used to validate this. ETAG is the checksum of the resource. For this Client should maintain old ETAG value somehow. If the checksum of resource has changed then the new resource with HTTP 200 is sent else HTTP 304 (use local copy) is sent.

[Update]

PUT /companies/Nintendo

GET /companies

are two different resources. Your the cache for /companies/Nintendo is only expected to be updated and not /companies (I am talking of client side cache) when PUT /companies/Nintendo request is executed. Suppose you call GET /companies/Nintendo next time, based on http headers the response is returned. GET /companies is a brand new request as it points to different resource.

Now question is what should be the http headers? It is purely application specific. Suppose it is stock quote I would not cache. Suppose it is NEWS item I would cache for certain time. Your reference link http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html has all the details of Cache http headers. Only thing not mentioned much is ETag usage. ETag can have checksum of resource. Check http://en.wikipedia.org/wiki/HTTP_ETag and also check https://devcenter.heroku.com/articles/increasing-application-performance-with-http-cache-headers

Satish
  • 713
  • 1
  • 5
  • 18
  • The key point in your answer seems to be the quote from the article: `There is no way for the HTTP protocol to guarantee that all such cache entries are marked invalid.` The remaining question is what is the proper course of action (according to REST)? – Gili Oct 04 '13 at 06:35
  • Your 3rd Point Every time I create a new company, I want to invalidate the cache associated with GET /companies. I explained how you can achieve this for server cache. For browser cache checking you may have to use ETAGS. – Satish Oct 04 '13 at 06:44
  • This question was never about a server-side cache (the article I linked to is about client-side caches). The question already mentions the possible use of `ETag` but it's not clear what other caching headers should be returned alongside the `ETag`. I'm looking for references to a best-practice, not just a "this works for me" solution. – Gili Oct 04 '13 at 06:54
  • Sorry Satish, you're not understanding my question. I know how `ETag` work (otherwise I wouldn't have mentioned it in the question) and I know that we're talking about two different resources. I am asking whether it is possible for me to instruct a browser to invalidate the cache for an arbitrary URI. That's it, that's all. – Gili Oct 04 '13 at 17:35