Database-backed cache for OpenURI
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


A database-backed HTTP caching library for open_uri. Just use open_uri
like normal and any URL's requested more than once will be served from
the database to speed up requests and be a better net citizen to other
web servers.


So with the standard library you do something like:

    require 'open-uri'
    puts open(url).read

With OpenURI::DbCache you change the above code to:

    require 'open_uri_db_cache'
    puts open(url).read


To manually clear the cache for a specific feed use the following code:


To completely clear the cache for all URL's the following code can be used:



A major goal of this library is for the most part not have to deal with
it. Just include the gem and continue as normal. But we do have a few
bit of configuration available

    # Configure how long to wait before even trying to fetch again.
    # After this time period If-Modified-Since and ETag checking is
    # still done. But until this time has passed we don't even bother
    # checking those and just assume the cache is good.
    OpenURI::DbCache::Page.fetch_limit = 8.hours

    # Configure what mime types should be cached. Is an array of
    # regexps with the default being:
    #     [/text/, /xml/] 
    # This is used primarily to ensure we are only caching non-binary
    # resources as most database do not support storing binary data
    # (or at least they do so in inconsistant ways). Since the primary
    # purpose of this library is to cache things like RSS feeds
    # restricting to non-binary data is ok.
    OpenURI::DbCache::Page.cache_types << /foo/

There are also some other options that you can configure using standard
ActiveRecord methods.


The cache is stored in the database and ActiveRecord is used to do
the updating. All tables used by the library are prefixed with the
string "open_uri_cache_".

If the tables are not detected in your database the library will
automatically create the tables so no migration script needs to be
run. You can manually setup the database by calling:


NOTE: Just because this library uses ActiveRecord does not mean you
have to use ActiveRecord but if you are not using ActiveRecord you will
need to establish a connection to some database prior to using the
library. So:

    ActiveRecord::Base.establish_connection ....

If you don't like the idea of storing in a database you can always just
point it to a SQLite file or even an in-memory SQLite database.

= Similar Libraries

There are some other libraries that provide similar functionality. If
this library does not meet your needs you might want to check them out.

    Uses memcached instead of a database backend. Also has optional
    integration with the Rails cache. Does NOT take If-Modified-Since
    and ETag headers into account when expiring the cache.

    Uses a filesystem cache instead of a database backend. Does NOT
    take If-Modified-Since and ETag headers into account.


This gem was developed by Eric Anderson ( for
the call recording service (
Some minor enhancements were done while working under Red Tusk
Studios (