Class: CacheMethod::CachedResult

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

Overview

:nodoc: all

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CachedResult



11
12
13
14
15
# File 'lib/cache_method/cached_result.rb', line 11

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



20
21
22
# File 'lib/cache_method/cached_result.rb', line 20

def args
  @args
end

#method_idObject (readonly)

Returns the value of attribute method_id.



18
19
20
# File 'lib/cache_method/cached_result.rb', line 18

def method_id
  @method_id
end

#objObject (readonly)

Returns the value of attribute obj.



17
18
19
# File 'lib/cache_method/cached_result.rb', line 17

def obj
  @obj
end

#original_method_idObject (readonly)

Returns the value of attribute original_method_id.



19
20
21
# File 'lib/cache_method/cached_result.rb', line 19

def original_method_id
  @original_method_id
end

Class Method Details

.fetch(options = {}) ⇒ Object



5
6
7
8
# File 'lib/cache_method/cached_result.rb', line 5

def fetch(options = {})
  cached_result = new options
  cached_result.fetch
end

Instance Method Details

#args_digestObject



53
54
55
# File 'lib/cache_method/cached_result.rb', line 53

def args_digest
  @args_digest ||= args.empty? ? 'empty' : ::Digest::MD5.hexdigest(args.join)
end

#cache_keyObject



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

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

#current_epochObject



57
58
59
# File 'lib/cache_method/cached_result.rb', line 57

def current_epoch
  @current_epoch ||= Epoch.current(:obj => obj, :method_id => method_id)
end

#fetchObject

Store things wrapped in an Array so that nil is accepted



23
24
25
26
27
28
29
30
31
# File 'lib/cache_method/cached_result.rb', line 23

def fetch
  if v = Config.instance.storage.get(cache_key) and v.is_a?(::Array)
    v[0]
  else
    v = obj.send original_method_id, *args
    Config.instance.storage.set cache_key, [v], ttl
    v
  end
end

#method_signatureObject



45
46
47
# File 'lib/cache_method/cached_result.rb', line 45

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

#obj_hashObject



49
50
51
# File 'lib/cache_method/cached_result.rb', line 49

def obj_hash
  @obj_hash ||= obj.hash
end

#ttlObject



33
34
35
# File 'lib/cache_method/cached_result.rb', line 33

def ttl
  @ttl ||= Config.instance.default_ttl
end