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

Returns a new instance of 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



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

def args_digest
  @args_digest ||= ::Digest::MD5.hexdigest(args.flatten.join)
end

#cache_keyObject



36
37
38
# File 'lib/cache_method/cached_result.rb', line 36

def cache_key
  [ method_signature, current_epoch, obj_hash, args_digest ].join ','
end

#current_epochObject



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

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

#fetchObject



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

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



40
41
42
# File 'lib/cache_method/cached_result.rb', line 40

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

#obj_hashObject



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

def obj_hash
  @obj_hash ||= obj.hash
end

#ttlObject



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

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