Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Cache method calls and speed up your Ruby on Rails application with MethodCacheable.
Ruby JavaScript
tag: v0.0.2

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.yardoc
autotest
doc
lib
pkg
spec
.gitignore
.rvmrc
Gemfile
Rakefile
VERSION
license.txt
method_cacheable.gemspec
readme.md

readme.md

The Cache in Black

Cache method calls and speed up your Ruby on Rails application with MethodCacheable.

Method Cacheable

  class User < ActiveRecord::Base
    include MethodCacheable

    has_many :pictures

    def expensive_method(val)
      sleep 120
      return val
    end
  end

  user = User.last

  # Call User#expensive_method normally
  user.expensive_method(22)
  # => 22

  # Fetch User#expensive_method from cache
  user.cache.expensive_method(22)
  # => 22

  # Call User#expensive_method normally
  Benchmark.measure { user.expensive_method(22) }.real
  # => 120.00037693977356

  # Fetch User#expensive_method from cache
  Benchmark.measure { user.cache.expensive_method(22) }.real
  # => 0.000840902328491211

  # SOOOOOOOO FAST!!

Install

in your Gemfile

gem 'method_cacheable'

then in your models

include MethodCacheable

Usage

Explicitly write & read methods.

  user.cache(:read).pictures  # => nil
  user.cache(:write).pictures # => [<# Picture ...>, <# Picture ...>] # refreshes the cache
  user.cache(:read).pictures  # => [<# Picture ...>, <# Picture ...>]

By default the cache method will will :fetch from the cache store. This means that if the key exists it will be pulled, if not the method will be called, returned, and the key will be set.

  user.cache(:read).expensive_method("w00t") # => nil
  user.cache.expensive_method("w00t")        # => "w00t" # sets the cache via :fetch
  user.cache.expensive_method("w00t")        # => "w00t" # pulls from the cache

You can also call :fetch explicitly if you prefer (but why, thats more typing)

  user.cache(:fetch).expensive_method("w00t")  # => "w00t" # pulls from the cache

Different method arguments to the method generate different cache objects. I.E. different input => different output, same input => same output

  user.cache.expensive_method(:schneems => :is_awesome).inspect
  # => {:schneems => :is_awesome}
  user.cache.expensive_method("j/k lol").inspect
  # => "j/k lol"

Configuration

Any configuration options passed to the cache method will be passed to the cache store (default is Rails.cache)

  user.cache(:write, :expires_in => 5.seconds).pictures # => [<# Picture ...>, #... ]
  user.cache(:read).pictures                            # => [<# Picture ...>, #... ]
  sleep 10                                              # => 2
  user.cache(:read).pictures                            # => nil

Contribution

Fork away. If you want to chat about a feature idea, or a question you can find me on the twitters @schneems. Put any major changes into feature branches. Make sure all tests stay green, and make sure your changes are covered.

licensed under MIT License Copyright (c) 2011 Schneems. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.