class LockAndCache::Key

@private

Constants

ALLOWED_IN_KEYS
DATE
METHOD_NAME_IN_CALLER

Attributes

context[R]
method_id[R]

Public Class Methods

extract_class_name(context) click to toggle source

@private

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

# File lib/lock_and_cache/key.rb, line 19
def extract_class_name(context)
  (context.class == ::Class) ? context.name : context.class.name
end
extract_method_id_from_caller(kaller) click to toggle source

@private

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

# File lib/lock_and_cache/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) click to toggle source

@private

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

# File lib/lock_and_cache/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
new(parts, options = {}) click to toggle source
# File lib/lock_and_cache/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

Public Instance Methods

cached?() click to toggle source
# File lib/lock_and_cache/key.rb, line 104
def cached?
  LockAndCache.cache_storage.exists? digest
end
class_name() click to toggle source
# File lib/lock_and_cache/key.rb, line 125
def class_name
  @class_name ||= Key.extract_class_name context
end
clear() click to toggle source
# File lib/lock_and_cache/key.rb, line 108
def clear
  LockAndCache.logger.debug { "[lock_and_cache] clear #{debug} #{Base64.encode64(digest).strip} #{Digest::SHA1.hexdigest digest}" }
  LockAndCache.cache_storage.del digest
  LockAndCache.lock_storage.del lock_digest
end
context_id() click to toggle source
# File lib/lock_and_cache/key.rb, line 116
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
debug()
Alias for: key
digest() click to toggle source

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

# File lib/lock_and_cache/key.rb, line 82
def digest
  @digest ||= ::Digest::SHA1.hexdigest ::Marshal.dump(key)
end
key() click to toggle source

A human-readable representation of the key parts

# File lib/lock_and_cache/key.rb, line 92
def key
  @key ||= if context
    [class_name, context_id, method_id, parts].compact
  else
    parts
  end
end
Also aliased as: debug
lock_digest() click to toggle source

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

# File lib/lock_and_cache/key.rb, line 87
def lock_digest
  @lock_digest ||= 'lock/' + digest
end
locked?() click to toggle source
# File lib/lock_and_cache/key.rb, line 100
def locked?
  LockAndCache.lock_storage.exists? lock_digest
end
parts() click to toggle source

An array of the parts we use for the key

# File lib/lock_and_cache/key.rb, line 130
def parts
  @parts ||= Key.extract_obj_id @_parts
end