Rack cache headers
George Malamidis, November 8th, 2008Rack is an interface between web servers and Ruby web frameworks. The HTTP protocol, amongst other things, defines requirements on HTTP caches in terms of header fields that control cache behavior. The purpose of this article is to demonstrate a possible implementation of a piece of Rack Middleware which enables web application developers to configure a web application’s resource cache related headers in a non obtrusive, centralized manner.
Rack supports the notion of Middleware, pieces of code that sit between the HTTP request and response life cycle. Rack::Lint, for example, validates an application’s requests and responses according to the Rack specification.
Rack::Handler::Mongrel.run( Rack::Lint.new(app), :Host => "0.0.0.0",ort => 9999 )
Similarly, if we were to implement a cache header producing layer on top of Rack we’d end up with a construct similar to the following.
Rack::Handler::Mongrel.run(
Rack::Lint.new(
Rack::CacheHeaders.new(app)
), :Host => "0.0.0.0",
ort => 9999
)
Here’s a possible way of configuring how an application provides HTTP caching headers based on URL path patterns.
Rack::CacheHeaders.configure do |cache|
cache.max_age("/rock", 3600)
cache.expires("/metal", "16:00")
end
Following is a potential implementation for the above.
module Rack
class CacheHeaders
def initialize(app)
@app = app
end
def call(env)
result = @app.call(env)
header = Configuration[env['PATH_INFO']].to_header
result[1][header.key] = header.value
result
end
def self.configure(&block)
yield Configuration
end
class Configuration
def self.max_age(path, duration)
paths[path] = MaxAge.new(duration)
end
def self.expires(path, date)
paths[path] = Expires.new(date)
end
def self.[](key)
paths[key]
end
def self.paths
@paths ||= {}
end
end
class MaxAge
def initialize(duration)
@duration = duration
end
def to_header
Header.new("Cache-Control", "max-age=#{@duration}, must-revalidate")
end
end
class Expires
def initialize(date)
@date = date
end
def to_header
Header.new("Expires", Time.parse(@date).httpdate)
end
end
class Header < Struct.new(:key, :value);end
end
end
The code below is a minimal Rack based application.
require "rubygems"
require "rack"
app = proc {|env| [200, {"Content-Type" => "text/plain"}, "hello"]}
Rack::Handler::Mongrel.run(
Rack::Lint.new(
Rack::CacheHeaders.new(app)
), :Host => "0.0.0.0",
ort => 9999
)
In order to observe the caching related headers the application’s responses are decorated with we can use curl or something similar, i.e curl -I http://0.0.0.0:9999/rock or curl -I http://0.0.0.0:9999/metal. Output should look something like the following.
air:~ gmalamid$ curl -I http://0.0.0.0:9999/rock HTTP/1.1 200 OK Connection: close Date: Sat, 08 Nov 2008 00:51:23 GMT Cache-Control: max-age=3600, must-revalidate Content-Type: text/plain Content-Length: 5 air:~ gmalamid$ curl -I http://0.0.0.0:9999/metal HTTP/1.1 200 OK Connection: close Date: Sat, 08 Nov 2008 00:51:16 GMT Content-Type: text/plain Expires: Sat, 08 Nov 2008 16:00:00 GMT Content-Length: 5
Understanding and employing HTTP cache configuration not only enables harnessing the power of tools like Varnish or Squid, it also makes good citizens in a diverse ecosystem of HTTP aware browsers and caches outside an application’s knowledge or control.

November 8th, 2008 at 4:26 am
How is this any different then Rack::Cache?
November 8th, 2008 at 12:34 pm
Hi Justin,I might be missing something, but as far as I understand it, Rack::Cache deals with cache storage based on an application’s response cache headers. This is a means for configuring those headers per URI path.
November 8th, 2008 at 5:39 pm
Nice. There’s a configuration system in Rack::Cache and specifying cache policy was one of the things I had envisioned it being used for, but this looks really simple and elegant.
Also, regarding Justin’s comment – I’ve noticed that a lot of people seem to have the impression that Rack::Cache was designed to automatically set the ETag, Last-Modified, Cache-Control, and Expires headers for an full-blown caching server upstream. In reality, Rack::Cache is a full blown cache. It uses the headers set by your app to implement the caching semantics defined in RFC 2616. It actually implements more of the spec than Varnish.
Clearly, I have some work to do in order to convey all this.
May 18th, 2009 at 1:22 pm
[...] few months ago I wrote about a possible method for centrally configuring HTTP cache headers in Rack based web applications [...]