Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Cachedis caches expensive (database) queries in Redis
Branch: master
Failed to load latest commit information.
lib Inital rewrite
test Inital rewrite
.rspec Initial working version
Gemfile Initial commit
Gemfile.lock Inital rewrite
Rakefile Initial working version
cachedis.gemspec Inital rewrite


Cachedis caches your expensive queries to a Redis instance so the next time you fire it off, it'll load directly from cache instead of being run again.

cachedis 'expensive:query', :expire => 60 * 60  do


cachedis takes two arguments in addition to a block which should return whatever you want cachedis to cache:

key, options = {}

The key is the name of the key under which the objects are saved as in Redis. Options are send directly to the Redis instance in along. For instance the option could be expiring the key at a certain time:

include Cachedis::Interface

# expire in an hour using Redis' EXPIRE command
cachedis 'users:all:with_avatars', :expire => 60 * 60 * 60 do

# expire at unix timestamp with Redis' EXPIREAT command
cachedis 'expensive:query', :expireat =>,2,2).to_i do
  # insert expensive query here


If you want to configure the server(s) or port(s) for cachedis to use, you should override the default Cachedis::Interface#cachedis method. Whatever you pass to #new is passed directly to the instance of the Redis object from the redis-rb library:

def cachedis(name, options = {}, &block)
  @cachedis ||= => "", :port => 6380)
  @cachedis.cachedis(name, options, &block)

Installation and dependencies

Dependencies: redis-rb

Install: gem install cachedis

Redis should be running in the background, start it with redis-server.


I experimented and benchmarked with JSON, YAML and Marshal using the drivers in the Ruby standard libary with this script.

Marshal is about 2x as fast as the other formats. However, it's also the one that is gonna take up the most space, but it's so little that it should not matter.

You can switch serialization driver if you need to by overriding Cachedis.serializer. The serializer must respond to #load and #dump, remember to require it before using it with cachedis:

module Cachedis
  def self.serializer

Wishlist/To do/To consider

  • ActiveRecord integration
    • Post.all.expensive_operation.cachedis
  • Make expirement time optional
  • Sexify the API
    • Make it easier to specialize expirement time (e.g. :expire => 4.hours), or just let this be for ActiveSupport users only?
  • Rename Cachedis#cachedis?
Something went wrong with that request. Please try again.