module MethodCacheable

include MethodCacheable

@example

class User < ActiveRecord::Base
  include MethodCacheable

  def expensive_method(val)
    sleep 120
    return val
  end
end

user = User.last

user.expensive_method(22)
# => 22
user.cache.expensive_method(22)
# => 22

Benchmark.measure do
  user.expensive_method(22)
end.real
# => 120.00037693977356

Benchmark.measure do
  user.cache.expensive_method(22)
end.real
# => 0.000840902328491211

# SOOOOOOOO FAST!!

@see MethodCacheable#cache More info on cache options

Public Class Methods

config() { |self| ... } click to toggle source
# File lib/method_cacheable.rb, line 43
def self.config
  yield self
end

Public Instance Methods

cache(*args) click to toggle source

@overload cache @overload cache(options = {}) @overload cache(cache_operation = :fetch) @overload cache(cache_operation = :fetch, options = {})

Creates a MethodCache instance that performs the given cache operation on the method it receives
@param [Symbol] cache_operation (:fetch) The method called on the cache (:write, :read, or :fetch)
@param [Hash] options Optional hash that gets passed to the cache store

@example

user = User.last
# cache
user.cache.some_method

# cache(options)
user.cache(:expires_in => 1.minutes).some_method

# cache(cache_operation)
user.cache(:fetch).some_method
user.cache(:read).some_method
user.cache(:write).some_method

# cache(cache_operation, options)
user.cache(:write, :expires_in 2.minutes).some_method
# File lib/method_cacheable.rb, line 73
def cache(*args)
  MethodCache.new(self, *args)
end