I am dealing with a group of pages that display data from a few
different HTTP resources. In order to get these pages to be performant
you must understand that I want to cache as much as possible at the
front of my Rails application.
Another requirement is that we never show stale data to the user.
These pages aren't getting huge amounts of requests/second, but when
there are no changes I want everything to feel snappy. If it takes a
bit more time for the first requests to set up the caches it's okay.
Fortunately HTTP ships with something awesome since the 80's:
conditional HTTP request. Just send an 'If-Modified-Since' or 'If-None-
Match' header along with the request and the server returns a '304 Not
Modified' or the full response.
It is simple to use the Rails Cache Store to cache HTTP responses. But
actually, the most time consuming is building an object model from the
response and generate the HTML fragments.
Therefore I am looking for an API that allows me to conditionally
execute render code, based on the response of external HTTP requests.
[browser] --[GET /page]--> [Rails app][view][controller][model] --
[GET /resource]--> [external service]
I tried to come up with a first proposal: https://gist.github.com/1383983
What do you guys think? Does this make any sense? Are there any other
approaches that I could try?
At least it provides a way through different layers, without leaking
knowledge. The downside is of course that every finder needs support a
block, where it normally would just return a result value.
This approach makes it impossible to cache different HTML fragments of
the same resource, but I think I can mitigate that in a followup
Thank you for providing feedback,