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

#autorefresh, #refresh, #refresh_later

Constructor Details

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

Returns a new instance of CachedMethod.



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

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

Instance Attribute Details

#autorefresh_blockObject

Returns the value of attribute autorefresh_block.



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

def autorefresh_block
  @autorefresh_block
end

#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



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

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

#call(target) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/cache_keeper/cached_method.rb', line 22

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

#stale?(target) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/models/cache_keeper/cached_method.rb', line 18

def stale?(target)
  cache_entry(target).blank? || cache_entry(target).expired?
end