Module: MethodCacheable

Extended by:
ActiveSupport::Concern
Defined in:
lib/method_cacheable.rb

Overview

include MethodCacheable

Examples:

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 Also:

Defined Under Namespace

Modules: ClassMethods Classes: MethodCache

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



43
44
45
# File 'lib/method_cacheable.rb', line 43

def self.config
  yield self
end

Instance Method Details

#cacheObject #cache(options = {}) ⇒ Object #cache(cache_operation = :fetch) ⇒ Object #cache(cache_operation = :fetch, options = {}) ⇒ Object

Examples:

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

Overloads:

  • #cache(cache_operation = :fetch, options = {}) ⇒ Object

    Creates a MethodCache instance that performs the given cache operation on the method it receives

    Parameters:

    • cache_operation (Symbol) (defaults to: :fetch)

      (:fetch) The method called on the cache (:write, :read, or :fetch)

    • options (Hash) (defaults to: {})

      Optional hash that gets passed to the cache store



73
74
75
# File 'lib/method_cacheable.rb', line 73

def cache(*args)
  MethodCache.new(self, *args)
end