Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Cache results of timeconsuming stuff in redis leveraging redis expiration.

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rvmrc
Octocat-spinner-32 Gemfile
Octocat-spinner-32 README.markdown
Octocat-spinner-32 Rakefile
Octocat-spinner-32 simple_redis_cache.gemspec


Easily cache the result of a block within redis for a given time period.

Let's cache something for 1 minute

    class MyClass

      include SimpleRedisCache

      def hello
        cache('hello', :ttl=>60){ 'world' }

Call it straight from the module instead and this time cache it forever.

    SimpleRedisCache.cache('hello') { 'world' }

Maybe we should cache something worth while. If the result of the block is not a string, make sure to convert it to one.

  require 'json'

  JSON.parse(cache('20_perm_4', :ttl=>120){ (0..20).to_a.permutation(4).to_a.to_json })

Notice we serialize to json inside the block and from json outside the block. Of course converting an enormous array to and from JSON is slower than the permutation so maybe it's not the best example. Replace the above with an expensive databse calculation and it makes more sense.

The above uses the default instance running on

Let's configure a different redis instance.

  SimpleRedisCache::Config.redis = => "", :port => 6380)

My benchmarks show this is faster than your current cache.

But why would I use this?

Because you already depend on redis through Resque.

Something went wrong with that request. Please try again.