Smooth HTTP Caching With Rack::Cache
The ways to cache a web application are numerous and often complex. Basic Rails page caching can become tedious to manage as your application infrastructure grows.
Rails 2.2 introduced conditional GET through the use of http headers: last_modified and etag. Following internet standard caching section of RFC2616, Ryan Tomayko introduced Rack::Cache.
Rack::Cache is a piece of Rack middleware that implements most of RFC 2616's caching features with a basic set of storage options (disk, heap, and memcached) and a configuration system for tweaking cache policy. It should work equally well with any Rack-enabled Ruby web framework and is tested on Ruby 1.8.6 and 1.8.7.
Part of its design was inspired by the Python cache framework Django.
As Ryan King states, you can migrate smoothly to real gateway proxy if your application really needs it:
Once your application gets larger and more complex, it makes sense to use an http reverse proxy cache like squid or varnish, but transitioning from rails-style page caching to HTTP caching is non-trivial. You have to change your deployment and your application in significant ways. Not fun.
With Rack::Cache you only have to change your deployment. You can even do it incrementally. You could start by using Rack::Cache to cache in the heap, later switch to the filesystem and finally to memcache. When that reaches its scalability limits you can add squid or varnhish in front of your application, then remove Rack::Cache. Deployments that involve only one major change at each step are much easier to get right than trying to make several big changes in a single operation.
It will be interesting to have a look at Ryan's upcoming benchmarks.
Jim Driscoll Dec 08, 2013