Class: CacheMethod::CachedResult

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

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/cache_method/cached_result.rb', line 3

def initialize(obj, method_id, original_method_id, ttl, args, &blk)
  @obj = obj
  @method_id = method_id
  @method_signature = CacheMethod.method_signature obj, method_id
  @original_method_id = original_method_id
  @ttl = ttl || CacheMethod.config.default_ttl
  @args = args
  @args_digest = args.empty? ? 'empty' : CacheMethod.digest(args)
  @blk = blk
  @fetch_mutex = ::Mutex.new
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#args_digestObject (readonly)

Returns the value of attribute args_digest.



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

def args_digest
  @args_digest
end

#blkObject (readonly)

Returns the value of attribute blk.



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

def blk
  @blk
end

#method_idObject (readonly)

Returns the value of attribute method_id.



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

def method_id
  @method_id
end

#method_signatureObject (readonly)

Returns the value of attribute method_signature.



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

def method_signature
  @method_signature
end

#objObject (readonly)

Returns the value of attribute obj.



15
16
17
# File 'lib/cache_method/cached_result.rb', line 15

def obj
  @obj
end

#original_method_idObject (readonly)

Returns the value of attribute original_method_id.



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

def original_method_id
  @original_method_id
end

#ttlObject (readonly)

Returns the value of attribute ttl.



22
23
24
# File 'lib/cache_method/cached_result.rb', line 22

def ttl
  @ttl
end

Instance Method Details

#debug_get_wrappedObject Also known as: get_wrapped



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cache_method/debug.rb', line 13

def debug_get_wrapped
  retval = original_get_wrapped
  if retval
    # $stderr.puts
    # $stderr.puts "[cache_method] GET: #{method_signature}(#{args.inspect})"
  else
    cache_key = CacheMethod.resolve_cache_key obj
    m = Marshal.dump cache_key
    $stderr.puts
    $stderr.puts "[cache_method] GET (miss!): #{method_signature}(#{args.inspect}) - #{::Digest::SHA1.hexdigest(m)} - #{cache_key.inspect}"
  end
  retval
end

#debug_set_wrappedObject Also known as: set_wrapped



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cache_method/debug.rb', line 29

def debug_set_wrapped
  retval = original_set_wrapped
  m = Marshal.dump retval
  if m.length > 1000
    $stderr.puts
    $stderr.puts "[cache_method] SET (#{'X' * (m.length / 1024)}): #{method_signature}(#{args.inspect}) -> #{retval.inspect}"
  else
    # $stderr.puts
    # $stderr.puts "[cache_method] SET: #{method_signature}(#{args.inspect})"
  end
  retval
end

#exist?Boolean



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

def exist?
  CacheMethod.config.storage.exist?(cache_key)
end

#fetchObject

Store things wrapped in an Array so that nil is accepted



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cache_method/cached_result.rb', line 25

def fetch
  if wrapped_v = get_wrapped
    wrapped_v.first
  else
    if @fetch_mutex.try_lock
      # i got the lock, so don't bother trying to get first
      begin
        set_wrapped.first
      ensure
        @fetch_mutex.unlock
      end
    else
      # i didn't get the lock, so get in line, and do try to get first
      @fetch_mutex.synchronize do
        (get_wrapped || set_wrapped).first
      end
    end
  end
end

#original_get_wrappedObject



26
# File 'lib/cache_method/debug.rb', line 26

alias :original_get_wrapped :get_wrapped

#original_set_wrappedObject



41
# File 'lib/cache_method/debug.rb', line 41

alias :original_set_wrapped :set_wrapped