Class: CacheKeeper::CachedMethod

Inherits:
Object
  • Object
show all
Includes:
Refreshable, SerializableTarget
Defined in:
app/models/cache_keeper/cached_method.rb

Defined Under Namespace

Modules: Refreshable, SerializableTarget

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SerializableTarget

#serialize_target, #serialize_target?

Methods included from Refreshable

#refresh, #refresh_later

Constructor Details

#initialize(klass, method_name, options = {}) ⇒ CachedMethod

Returns a new instance of CachedMethod.



7
8
9
10
11
# File 'app/models/cache_keeper/cached_method.rb', line 7

def initialize(klass, method_name, options = {})
  self.klass = klass
  self.method_name = method_name
  self.options = options.with_indifferent_access
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



5
6
7
# File 'app/models/cache_keeper/cached_method.rb', line 5

def klass
  @klass
end

#method_nameObject

Returns the value of attribute method_name.



5
6
7
# File 'app/models/cache_keeper/cached_method.rb', line 5

def method_name
  @method_name
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'app/models/cache_keeper/cached_method.rb', line 5

def options
  @options
end

Instance Method Details

#alias_for_original_methodObject



13
14
15
# File 'app/models/cache_keeper/cached_method.rb', line 13

def alias_for_original_method
  :"__#{method_name}__hooked__"
end

#call(target) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/cache_keeper/cached_method.rb', line 17

def call(target)
  cache_entry = cache_entry(target)

  if cache_entry.blank?
    refresh target
  elsif cache_entry.expired?
    if must_revalidate?
      refresh target
    else
      refresh_later target

      cache_entry.value
    end
  else
    cache_entry.value
  end
end