Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
55 lines (33 sloc) 1.3 KB


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.