method_caching

method_caching is a simple Ruby library for caching instance method of a class (using Rails.cache as cache store)

Installation

gem install method_caching

or add to Gemfile

gem "method_caching"

Usage

With ActiveRecord

class User < ActiveRecord::Base
  method_caching

  def full_name
    "#{first_name} #{last_name}"
  end
  cache_method :full_name, clear_on: :save # Clear cache for method full_name whenever method 'save' is called
end

Without ActiveRecord (Mongoid, etc)

class User
  include MethodCaching::Generic
  method_caching

  def full_name
    "#{first_name} #{last_name}"
  end
  cache_method :full_name, clear_on: :save # Clear cache for method full_name whenever method 'save' is called
end

Custom Identifier

By default, method_caching will use the record's id as the default identifier to generate the cache key. You can change the default identifier by passing custom identifier

class User < ActiveRecord::Base
  method_caching :custom_identifier
end

Contributing to method_caching

Copyright © 2013 Huy Ha. See LICENSE.txt for further details.