BT

Smooth HTTP Caching With Rack::Cache

by Sebastien Auvray on Nov 16, 2008 |

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.

Rack::Cache acts as a gateway proxy (Varnish , Squid) without all the pain to settle it. Rack::Cache supports expiration-based caching, validation model, and Vary header fields.

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.

Hello stranger!

You need to Register an InfoQ account or to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2013 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT