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