3

So in one of my projects i have to create a http cache to handle multiple API calls to the server. I read about this ETag header that can be used with a conditional GET to minimize server load and enact caching.. However i have a problem with generating the E-Tag.. I can use the LAST_UPDATED_TIMESTAMP of the resource as the ETag or hash it using some sort of hashing algo like MD5. but what would be the best way to do this? Is there any cons in using raw timestamp as the Etag??

any supportive answer is highly appreciated .. Thanks in advance....Cheers!!

Infamous
  • 744
  • 11
  • 25

1 Answers1

3

If your timestamp has enough precision so that you can guarantee it will change any time the resource changes, then you can use an encoding of the timestamp (the header value needs to be ascii).

But bear in mind that ETag may not save you much. It's just a cache revalidation header, so you will still get as many requests from clients, just some will be conditional, and you may then be able to avoid sending payload back if the ETag didn't change, but you will still incur some work figuring that out (maybe a bunch less work, so could be worth it).

In fact several versions of IIS used the file timestamp to generate an Etag. We tripped over that when building WinGate's cache module, when a whole bunch of files with the same timestmap ended up with the same Etag, and we learned that an Etag is only valid in the context of the request URI.

Adrien
  • 1,061
  • 8
  • 11
  • Could you further explain the Etag being valid in the context of URI? How did you get collisions? Shouldn't each file have a different URI? – otterslide Mar 03 '18 at 17:19
  • 1
    2 different URIs can have the same ETag value. Obviously they are different resources. So for example you cannot key on ETag. Earlier versions of IIS used the file modified date to generate the ETag, so if you copied a bunch of files into a folder, and they got the same date, then they could (and I've seen it) end up with the same ETag emitted by IIS. – Adrien Mar 03 '18 at 22:44
  • Ok, I think you are saying ETags should be unique for all resources on a domain, because URI/URL is not taken in account by the browser? – otterslide Mar 05 '18 at 00:11
  • 1
    No I’m not saying that. – Adrien Mar 05 '18 at 17:41