Class: CacheMethod::Epoch

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

Overview

:nodoc: all

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Epoch

Returns a new instance of Epoch.



19
20
21
22
23
# File 'lib/cache_method/epoch.rb', line 19

def initialize(options = {})
  options.each do |k, v|
    instance_variable_set "@#{k}", v
  end
end

Instance Attribute Details

#method_idObject (readonly)

Returns the value of attribute method_id.



26
27
28
# File 'lib/cache_method/epoch.rb', line 26

def method_id
  @method_id
end

#objObject (readonly)

Returns the value of attribute obj.



25
26
27
# File 'lib/cache_method/epoch.rb', line 25

def obj
  @obj
end

Class Method Details

.current(options = {}) ⇒ Object



4
5
6
7
# File 'lib/cache_method/epoch.rb', line 4

def current(options = {})
  epoch = new options
  epoch.current
end

.mark_passing(options = {}) ⇒ Object



9
10
11
12
# File 'lib/cache_method/epoch.rb', line 9

def mark_passing(options = {})
  epoch = new options
  epoch.mark_passing
end

.random_nameObject



14
15
16
# File 'lib/cache_method/epoch.rb', line 14

def random_name
  rand(1_000_000).to_s
end

Instance Method Details

#cache_keyObject



36
37
38
39
40
41
42
# File 'lib/cache_method/epoch.rb', line 36

def cache_key
  if obj.is_a? ::Class
    [ 'CacheMethod', 'Epoch', method_signature ].join ','
  else
    [ 'CacheMethod', 'Epoch', method_signature, obj_hash ].join ','
  end
end

#currentObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/cache_method/epoch.rb', line 44

def current
  if cached_v = Config.instance.storage.get(cache_key)
    cached_v
  else
    v = Epoch.random_name
    # never expire!
    Config.instance.storage.set cache_key, v, 0
    v
  end
end

#mark_passingObject



55
56
57
# File 'lib/cache_method/epoch.rb', line 55

def mark_passing
  Config.instance.storage.delete cache_key
end

#method_signatureObject



28
29
30
# File 'lib/cache_method/epoch.rb', line 28

def method_signature
  @method_signature ||= ::CacheMethod.method_signature(obj, method_id)
end

#obj_hashObject



32
33
34
# File 'lib/cache_method/epoch.rb', line 32

def obj_hash
  @obj_hash ||= obj.hash
end