Class: MethodCacheable::MethodCache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(caller, *method_cache_args) ⇒ MethodCache

Returns a new instance of MethodCache.



97
98
99
100
101
# File 'lib/method_cacheable.rb', line 97

def initialize(caller, *method_cache_args)
  self.caller          = caller
  self.cache_operation = method_cache_args.map {|x| x if x.is_a? Symbol }.compact.first||:fetch
  self.options         = method_cache_args.map {|x| x if x.is_a? Hash   }.compact.first
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 and used to :write, :read, or :fetch from the cache



177
178
179
180
181
182
183
184
185
# File 'lib/method_cacheable.rb', line 177

def method_missing(method, *args, &blk)
  self.method = method
  self.args   = args
  if caller.respond_to? method
    call
  else
    send_to_caller
  end
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



90
91
92
# File 'lib/method_cacheable.rb', line 90

def args
  @args
end

#cache_operationObject

Returns the value of attribute cache_operation.



90
91
92
# File 'lib/method_cacheable.rb', line 90

def cache_operation
  @cache_operation
end

#callerObject

Returns the value of attribute caller.



90
91
92
# File 'lib/method_cacheable.rb', line 90

def caller
  @caller
end

#methodObject

Returns the value of attribute method.



90
91
92
# File 'lib/method_cacheable.rb', line 90

def method
  @method
end

#optionsObject

Returns the value of attribute options.



90
91
92
# File 'lib/method_cacheable.rb', line 90

def options
  @options
end

Instance Method Details

#callObject

Calls the cache based on the given cache_operation

Parameters:

  • options (Hash)

    Options are passed to the cache store

See Also:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/method_cacheable.rb', line 106

def call
  case cache_operation
  when :fetch
    MethodCacheable.store.fetch(key, options) do
      send_to_caller
    end
  when :read
    read(method, args)
  when :write
    write(method, args) do
      send_to_caller
    end
  end
end

#delete(method, *args) ⇒ Object

Removes the current key from the cache store



159
160
161
162
163
# File 'lib/method_cacheable.rb', line 159

def delete(method, *args)
  self.method = method
  self.args   = args
  MethodCacheable.store.delete(key, options)
end

#exist?(method, *args) ⇒ boolean Also known as: exists?

Checks to see if the key exists in the cache store

Returns:

  • (boolean)


167
168
169
170
171
# File 'lib/method_cacheable.rb', line 167

def exist?(method, *args)
  self.method = method
  self.args   = args
  MethodCacheable.store.exist?(key)
end

#for(method, *args) ⇒ Object



134
135
136
137
138
# File 'lib/method_cacheable.rb', line 134

def for(method , *args)
  self.method = method
  self.args   = args
  self
end

#key(tmp_method = nil, *tmp_args) ⇒ Object

Uses keytar to create a key based on the method and caller if no method_key exits Method and arguement can optionally be supplied

Examples:

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

Returns:

  • the key used to set the cache

See Also:



150
151
152
153
154
155
156
# File 'lib/method_cacheable.rb', line 150

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

#read(method, *args) ⇒ Object



130
131
132
# File 'lib/method_cacheable.rb', line 130

def read(method, *args)
  MethodCacheable.store.read(key, options)
end

#send_to_callerObject



121
122
123
# File 'lib/method_cacheable.rb', line 121

def send_to_caller
  caller.send method.to_sym, *args
end

#write(method, *args, &block) ⇒ Object



125
126
127
128
# File 'lib/method_cacheable.rb', line 125

def write(method, *args, &block)
  val = block.call
  MethodCacheable.store.write(key, val, options)
end