Module: JohnnyCache

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

Overview

include JohnnyCache

Examples:

class User < ActiveRecord::Base
  include JohnnyCache

  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

Constant Summary collapse

STORE =
nil || Rails.cache

Instance Method Summary collapse

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



69
70
71
# File 'lib/johnny_cache.rb', line 69

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