Class: MethodCachable::MethodCache

Inherits:
Object
  • Object
show all
Defined in:
lib/method_cachable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(caller_object, *method_cache_args) ⇒ MethodCache

Returns a new instance of MethodCache.



86
87
88
89
90
91
92
# File 'lib/method_cachable.rb', line 86

def initialize(caller_object, *method_cache_args)
  cache_operation      = method_cache_args.map {|x| x if x.is_a? Symbol }.compact.first
  options           = method_cache_args.map {|x| x if x.is_a? Hash   }.compact.first
  self.cache_operation = cache_operation||:fetch
  self.options      = options
  self.caller_object       = caller_object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object

Methods caught by method_missing are passed to the caller_object and used to :write, :read, or :fetch from the cache



126
127
128
129
130
131
132
133
134
# File 'lib/method_cachable.rb', line 126

def method_missing(method, *args, &blk)
  if caller_object.respond_to? method
    self.method = method
    self.args = args
    call_cache_operation(options)
  else
    super
  end
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



84
85
86
# File 'lib/method_cachable.rb', line 84

def args
  @args
end

#cache_operationObject

Returns the value of attribute cache_operation.



84
85
86
# File 'lib/method_cachable.rb', line 84

def cache_operation
  @cache_operation
end

#caller_objectObject

Returns the value of attribute caller_object.



84
85
86
# File 'lib/method_cachable.rb', line 84

def caller_object
  @caller_object
end

#methodObject

Returns the value of attribute method.



84
85
86
# File 'lib/method_cachable.rb', line 84

def method
  @method
end

#optionsObject

Returns the value of attribute options.



84
85
86
# File 'lib/method_cachable.rb', line 84

def options
  @options
end

Instance Method Details

#call_cache_operation(options = {}) ⇒ Object

Calls the cache based on the given cache_operation

Parameters:

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

    Options are passed to the cache store

See Also:



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/method_cachable.rb', line 110

def call_cache_operation(options = {})
  if cache_operation == :fetch
    MethodCachable::STORE.fetch(key, options) do
      caller_object.send method.to_sym, *args
    end
  elsif cache_operation == :read
    MethodCachable::STORE.read(key, options)
  elsif cache_operation == :write
    val = caller_object.send method.to_sym, *args
    MethodCachable::STORE.write(key, val, options)
  end
end

#keyObject

Uses keytar to create a key based on the method and caller if no method_key exits

Examples:

cache = User.find(263619).cache   # => #<MethodCachable::MethodCache ... >
cache.method = "foo"              # => "foo"
cache.key                         # => "users:foo:263619"

Returns:

  • the key used to set the cache

See Also:



101
102
103
104
105
# File 'lib/method_cachable.rb', line 101

def key
  key_method = "#{method}_key".to_sym
  key = caller_object.send key_method, *args if caller_object.respond_to? key_method
  key ||= caller_object.build_key(:name => method, :args => args)
end