Class: JohnnyCache::MethodCache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(caller_object, *method_cache_args) ⇒ MethodCache

Returns a new instance of MethodCache.



88
89
90
91
92
93
94
# File 'lib/johnny_cache.rb', line 88

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

See Also:



128
129
130
131
132
133
134
135
136
# File 'lib/johnny_cache.rb', line 128

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.



86
87
88
# File 'lib/johnny_cache.rb', line 86

def args
  @args
end

#cache_operationObject

Returns the value of attribute cache_operation.



86
87
88
# File 'lib/johnny_cache.rb', line 86

def cache_operation
  @cache_operation
end

#caller_objectObject

Returns the value of attribute caller_object.



86
87
88
# File 'lib/johnny_cache.rb', line 86

def caller_object
  @caller_object
end

#methodObject

Returns the value of attribute method.



86
87
88
# File 'lib/johnny_cache.rb', line 86

def method
  @method
end

#optionsObject

Returns the value of attribute options.



86
87
88
# File 'lib/johnny_cache.rb', line 86

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:



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/johnny_cache.rb', line 112

def call_cache_operation(options = {})
  if cache_operation == :fetch
    JohnnyCache::STORE.fetch(key, options) do
      caller_object.send method.to_sym, *args
    end
  elsif cache_operation == :read
    JohnnyCache::STORE.read(key, options)
  elsif cache_operation == :write
    val = caller_object.send method.to_sym, *args
    JohnnyCache::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   # => #<JohnnyCache::MethodCache ... >
cache.method = "foo"              # => "foo"
cache.key                         # => "users:foo:263619"

Returns:

  • the key used to set the cache

See Also:



103
104
105
106
107
# File 'lib/johnny_cache.rb', line 103

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