Class: LockAndCacheMsgpack::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/lock_and_cache_msgpack/key.rb

Constant Summary collapse

ALLOWED_IN_KEYS =
[
  ::String,
  ::Symbol,
  ::Numeric,
  ::TrueClass,
  ::FalseClass,
  ::NilClass,
  ::Integer,
  ::Float,
].to_set
DATE =
[
  ::Date,
  ::DateTime,
  ::Time,
].to_set
METHOD_NAME_IN_CALLER =
/in `([^']+)'/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts, options = {}) ⇒ Key



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lock_and_cache_msgpack/key.rb', line 69

def initialize(parts, options = {})
  @_parts = parts
  @context = options[:context]
  @method_id = if options.has_key?(:method_id)
    options[:method_id]
  elsif options.has_key?(:caller)
    Key.extract_method_id_from_caller options[:caller]
  elsif context
    raise "supposed to call context with method_id or caller"
  end
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



66
67
68
# File 'lib/lock_and_cache_msgpack/key.rb', line 66

def context
  @context
end

#method_idObject (readonly)

Returns the value of attribute method_id.



67
68
69
# File 'lib/lock_and_cache_msgpack/key.rb', line 67

def method_id
  @method_id
end

Class Method Details

.extract_class_name(context) ⇒ Object

Get a context object’s class name, which is its own name if it’s an object.



19
20
21
# File 'lib/lock_and_cache_msgpack/key.rb', line 19

def extract_class_name(context)
  (context.class == ::Class) ? context.name : context.class.name
end

.extract_method_id_from_caller(kaller) ⇒ Object

Extract the method id from a method’s caller array.



10
11
12
13
14
# File 'lib/lock_and_cache_msgpack/key.rb', line 10

def extract_method_id_from_caller(kaller)
  kaller[0] =~ METHOD_NAME_IN_CALLER
  raise "couldn't get method_id from #{kaller[0]}" unless $1
  $1.to_sym
end

.extract_obj_id(obj) ⇒ Object

Recursively extract id from obj. Calls #lock_and_cache_key if available, otherwise #id



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lock_and_cache_msgpack/key.rb', line 26

def extract_obj_id(obj)
  klass = obj.class
  if ALLOWED_IN_KEYS.include?(klass)
    obj
  elsif DATE.include?(klass)
    obj.to_s
  elsif obj.respond_to?(:lock_and_cache_key)
    extract_obj_id obj.lock_and_cache_key
  elsif obj.respond_to?(:id)
    extract_obj_id obj.id
  elsif obj.respond_to?(:map)
    obj.map { |objj| extract_obj_id objj }
  else
    raise "#{obj.inspect} must respond to #lock_and_cache_key or #id"
  end
end

Instance Method Details

#cached?Boolean



104
105
106
# File 'lib/lock_and_cache_msgpack/key.rb', line 104

def cached?
  LockAndCacheMsgpack.storage.exists digest
end

#class_nameObject



126
127
128
# File 'lib/lock_and_cache_msgpack/key.rb', line 126

def class_name
  @class_name ||= Key.extract_class_name context
end

#clearObject



108
109
110
111
112
113
# File 'lib/lock_and_cache_msgpack/key.rb', line 108

def clear
  LockAndCacheMsgpack.logger.debug { "[lock_and_cache] clear #{debug} #{Base64.encode64(digest).strip} #{Digest::SHA1.hexdigest digest}" }
  storage = LockAndCacheMsgpack.storage
  storage.del digest
  storage.del lock_digest
end

#context_idObject



117
118
119
120
121
122
123
124
# File 'lib/lock_and_cache_msgpack/key.rb', line 117

def context_id
  return @context_id if defined?(@context_id)
  @context_id = if context.class == ::Class
    nil
  else
    Key.extract_obj_id context
  end
end

#digestObject

A (non-cryptographic) digest of the key parts for use as the cache key



82
83
84
# File 'lib/lock_and_cache_msgpack/key.rb', line 82

def digest
  @digest ||= ::Digest::SHA1.hexdigest MessagePack.pack(key)
end

#keyObject Also known as: debug

A human-readable representation of the key parts



92
93
94
95
96
97
98
# File 'lib/lock_and_cache_msgpack/key.rb', line 92

def key
  @key ||= if context
    [class_name, context_id, method_id, parts].compact
  else
    parts
  end
end

#lock_digestObject

A (non-cryptographic) digest of the key parts for use as the lock key



87
88
89
# File 'lib/lock_and_cache_msgpack/key.rb', line 87

def lock_digest
  @lock_digest ||= 'lock/' + digest
end

#locked?Boolean



100
101
102
# File 'lib/lock_and_cache_msgpack/key.rb', line 100

def locked?
  LockAndCacheMsgpack.storage.exists lock_digest
end

#partsObject

An array of the parts we use for the key



131
132
133
# File 'lib/lock_and_cache_msgpack/key.rb', line 131

def parts
  @parts ||= Key.extract_obj_id @_parts
end