Client-Side Caching with Cacheability

restclient rack caching

Tue Apr 07 17:52:25 -0700 2009

Cyril Rohr gives us Cacheability, a caching HTTP client which cleverly mashes together RestClient and Rack::Cache. It can cache results in memory, on the filesystem, or in memcached. Grab a copy:

$ gem install cryx-cacheability -s http://gems.github.com

And then try it:

require 'rubygems'
require 'cacheability/restclient'
require 'benchmark'

resource = RestClient::CacheableResource.new('http://heroku.com/')

printf "Pass 1 (cache miss): %0.04f\n", Benchmark.measure { resource.get }.real
printf "Pass 2 (cache hit):  %0.04f\n", Benchmark.measure { resource.get }.real

My results:

Pass 1 (cache miss): 0.1768
Pass 2 (cache hit):  0.0004

Of course the second pass is cached because the public content on heroku.com sets the proper Cache-control header. You do use HTTP caching for your public content, don’t you?