Class: CacheMethod::CachedResult

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

Overview

:nodoc: all

Constant Summary collapse

CACHE_KEY_JOINER =
','

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, method_id, original_method_id, ttl, args) ⇒ CachedResult

Returns a new instance of CachedResult.



33
34
35
36
37
38
39
# File 'lib/cache_method/cached_result.rb', line 33

def initialize(obj, method_id, original_method_id, ttl, args)
  @obj = obj
  @method_id = method_id
  @original_method_id = original_method_id
  @ttl = ttl
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#method_idObject (readonly)

Returns the value of attribute method_id.



42
43
44
# File 'lib/cache_method/cached_result.rb', line 42

def method_id
  @method_id
end

#objObject (readonly)

Returns the value of attribute obj.



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

def obj
  @obj
end

#original_method_idObject (readonly)

Returns the value of attribute original_method_id.



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

def original_method_id
  @original_method_id
end

Class Method Details

.resolve_cache_key(obj) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cache_method/cached_result.rb', line 5

def resolve_cache_key(obj)
  case obj
  when ::Array
    obj.map do |v|
      resolve_cache_key v
    end
  when ::Hash
    obj.inject({}) do |memo, (k, v)|
      kk = resolve_cache_key k
      vv = resolve_cache_key v
      memo[kk] = vv
      memo
    end
  else
    if obj.respond_to?(:to_cache_key)
      # this is meant to be used sparingly, usually when a proxy class is involved
      obj.to_cache_key
    elsif obj.respond_to?(:as_cache_key)
      [obj.class.name, obj.as_cache_key]
    else
      obj
    end
  end
end

Instance Method Details

#args_digestObject



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

def args_digest
  if (l = ::Marshal.dump(CachedResult.resolve_cache_key(args)).length) > 500
    $stderr.puts "Warn: #{l} long args digest. #{method_signature}(#{CachedResult.resolve_cache_key(args).inspect})"
  end
  @args_digest ||= args.empty? ? 'empty' : ::Digest::SHA1.hexdigest(::Marshal.dump(CachedResult.resolve_cache_key(args)))
end

#cache_keyObject



65
66
67
68
69
70
71
# File 'lib/cache_method/cached_result.rb', line 65

def cache_key
  if obj.is_a?(::Class) or obj.is_a?(::Module)
    [ 'CacheMethod', 'CachedResult', method_signature, current_generation, args_digest ].compact.join CACHE_KEY_JOINER
  else
    [ 'CacheMethod', 'CachedResult', method_signature, obj_digest, current_generation, args_digest ].compact.join CACHE_KEY_JOINER
  end
end

#current_generationObject



85
86
87
88
89
# File 'lib/cache_method/cached_result.rb', line 85

def current_generation
  if Config.instance.generational?
    @current_generation ||= Generation.new(obj, method_id).current
  end
end

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  Config.instance.storage.exist?(cache_key)
end

#fetchObject

Store things wrapped in an Array so that nil is accepted



47
48
49
50
51
52
53
54
55
# File 'lib/cache_method/cached_result.rb', line 47

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

#method_signatureObject



73
74
75
# File 'lib/cache_method/cached_result.rb', line 73

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

#obj_digestObject



3
4
5
6
7
8
# File 'lib/cache_method/debug.rb', line 3

def obj_digest
  if (l = ::Marshal.dump(CachedResult.resolve_cache_key(obj)).length) > 500
    $stderr.puts "Warn: #{l} long obj digest. #{obj.class} -> #{CachedResult.resolve_cache_key(obj).inspect}"
  end
  @obj_digest ||= ::Digest::SHA1.hexdigest(::Marshal.dump(CachedResult.resolve_cache_key(obj)))
end

#ttlObject



61
62
63
# File 'lib/cache_method/cached_result.rb', line 61

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