I've just read Kevin's answer to Do I need API key to use StackAuth API? and been quite surprised that the API doesn't seem to facilitate proper HTTP caching already:
You really shouldn't be hitting it more than once a day though, for site information, and in accordance with general API guidelines for associated users.
That's understandable and perfectly fine with me, but why don't you tell me (and especially my toolchain) this via the well established HTTP cache control headers then?
Both of which can be safely cached quite heavily.
Indeed, and I'd happily do so out of own interest, but preferably without the need to reinvent the wheel on my part for this well explored use case - if proper HTTP cache control headers would be served, any decent toolchain component wouldn't even think about violating your API guidelines/policies.
Granted, caching might not provide much benefit (if at all) for many volatile API routes and/or be not exactly easy to implement on the server side in some cases, but still it would for others, and the StackAuth routes are a good example, e.g. for /sites you could do:
- Expiration-based caching:
Send an HTTP 1.1Cache-Control: max-age=...header for one day into the future, if that's what the API guidelines ask for right now.- Of course you could get even fancy here in case you actually happen to know when you are going to update the sites, e.g. if you'd only ever publish a new site on a Friday you might as well indicate this right away.
- See HTTP 1.0
Expires: ...header too for the complete picture.
- Validation-based caching:
Send an HTTP 1.1ETag: ...header computed from a hash of the result.- This might not seem to be too useful with the short list of sites right now (though it would still reduce bandwidth considerably), but this is planned to change, isn't it ;) Even more important though is that you are still communicating valuable information, because otherwise I need to compare the current and former result locally myself in order to know whether there has been any change in this resource.
- See HTTP 1.0
Last Modified: ...header too for the complete picture.
Or any variation thereof - the chosen cache strategy largely depends on the particular route of course, but some basics applied would get us a long way without risking premature optimization, IMHO.
I'm reluctantly marking this as a feature request rather than a bug, but please consider applying these RESTful principles in order to take the cache control burden off the Stack Apps developer community shoulders and onto those of the Stack Exchange giants ;)
Further Reading
- HTTP caching options - more editorial like overview in the context of a RESTful architecture
- HTTP Caching - concise tutorial with good contrasting of expiration/validation
- How To Optimize Your Site With HTTP Caching - concise tutorial with good illustrations
- Caching Tutorial - thorough tutorial often cited/referenced elsewhere
- Caching in HTTP - the authoritative HTTP/1.1 reference
GET /what/it/is/asking/for HTTP/1.1 200, no? – Steffen Opel Jul 08 '10 at 11:00Cache-Control: privateheader already anyway, and it doesn't seem to break anything, why should addingmax-age=…possibly change that? I think you are contradicting yourself here, which leads me to the next point … – Steffen Opel Jul 08 '10 at 11:07GET /over/it HTTP/1.1 202and implement cache control properly in an upcoming point release :) – Steffen Opel Jul 08 '10 at 11:21Accept-Encodingand I have no desire to repeat a mistake of the past. In this particular case of purity versus pragmatism, pragmatism wins. Igor Zevaka answer outlines some additional arguments as to why caching headers wouldn't be that much of a net gain. Do note that the server already does substantial caching behind the scenes, we're just not pushing those headers as documented parts of the API for the outlined reasons. – Kevin Montrose Jul 08 '10 at 19:41Accept-Encodingrequires active participation from the client (in 'pure' mode), whileCache-Controldoes not (opt-in). It's not about purity, to the contrary, it's about a proven best practice design pattern for RESTful web services with a track record for performance and scalability. Thanks for answering at all, but I can't help but notice that you haven't answered a single specific question of mine (why can Twitter do it, why shouldCache-Controlbreak withmax-age=...etc.). Alas, looks like a lost case at the moment … – Steffen Opel Jul 08 '10 at 21:23