HTTP response caching
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Web is a great way to automatically record HTTP responses in a cache so you don't overwhelm an external service. You can also share the responses between multiple machines and multiple adapters (like typhoeus and net/http) seamlessly.

Web.register :get, /google\.com/

By doing that, any request is cached and will return as if it hit the web service directly. By default, the caching is to memory, but there are also adapters for caching via Memcached, Redis, or a custom adapter. (see below)

Web.register :any, /google\.com/
Net::HTTP.get_print URI.parse '' # from source
Net::HTTP.get_print URI.parse '' # from cache!
Typhoeus::Request.get '' # from cache!


You can also do

Web.register :get, /google\.com/, :expire => 2

To automatically expire requests to every 2 seconds

Different Caches

There are multiple cache classes, and you can add your own. MemoryCache caching is the default, but if you want to change it you can do something like:

Web.cache =

Existing Cache Classes

  • MemoryCache - Caches to memory directly
  • RedisCache - A cache backed by Redis
  • MemcachedCache - A cache backed by Memcached via Dalli

Custom Cache Classes

Custom caches are expected to respond to get(key) and set(key, value, expiry_s). lib/web/cache/redis_cache.rb is a great example implementation.


There is currently support for the following adapters:

  • typhoeus
  • net/http

It's easy to write new adapters, and the typhoeus adapter is a great example. Give it a read at lib/web/ext/typhoeus.rb and contribute one for another library, like curb or patron.

Seeing if a response is cached

Response objects from the adapters all have an added method called cached? that will return a boolean indicating whether or not the source of the response was the cache.


gem install web


John Crepezzi (