class ThreadSafe::Cache

Constants

KEY_ERROR

Public Class Methods

new(options = nil, &block) click to toggle source
Calls superclass method
# File lib/thread_safe/cache.rb, line 23
def initialize(options = nil, &block)
  if options.kind_of?(::Hash)
    validate_options_hash!(options)
  else
    options = nil
  end

  super(options)
  @default_proc = block
end

Public Instance Methods

[](key) click to toggle source
Calls superclass method
# File lib/thread_safe/cache.rb, line 34
def [](key)
  if value = super
    value
  elsif @default_proc && !key?(key)
    @default_proc.call(self, key)
  else
    value
  end
end
Also aliased as: get
each_key() { |k| ... } click to toggle source
# File lib/thread_safe/cache.rb, line 87
def each_key
  each_pair {|k, v| yield k}
end
each_value() { |v| ... } click to toggle source
# File lib/thread_safe/cache.rb, line 91
def each_value
  each_pair {|k, v| yield v}
end
empty?() click to toggle source
# File lib/thread_safe/cache.rb, line 95
def empty?
  each_pair {|k, v| return false}
  true
end
fetch(key, default_value = NULL) { |key| ... } click to toggle source
# File lib/thread_safe/cache.rb, line 47
def fetch(key, default_value = NULL)
  if NULL != (value = get_or_default(key, NULL))
    value
  elsif block_given?
    yield key
  elsif NULL != default_value
    default_value
  else
    raise KEY_ERROR, 'key not found'
  end
end
get(key)
Alias for: []
keys() click to toggle source
# File lib/thread_safe/cache.rb, line 75
def keys
  arr = []
  each_pair {|k, v| arr << k}
  arr
end
marshal_dump() click to toggle source
# File lib/thread_safe/cache.rb, line 106
def marshal_dump
  raise TypeError, "can't dump hash with default proc" if @default_proc
  h = {}
  each_pair {|k, v| h[k] = v}
  h
end
marshal_load(hash) click to toggle source
# File lib/thread_safe/cache.rb, line 113
def marshal_load(hash)
  initialize
  populate_from(hash)
end
put_if_absent(key, value) click to toggle source
# File lib/thread_safe/cache.rb, line 59
def put_if_absent(key, value)
  computed = false
  result = compute_if_absent(key) do
    computed = true
    value
  end
  computed ? nil : result
end
size() click to toggle source
# File lib/thread_safe/cache.rb, line 100
def size
  count = 0
  each_pair {|k, v| count += 1}
  count
end
value?(value) click to toggle source
# File lib/thread_safe/cache.rb, line 68
def value?(value)
  each_value do |v|
    return true if value.equal?(v)
  end
  false
end
values() click to toggle source
# File lib/thread_safe/cache.rb, line 81
def values
  arr = []
  each_pair {|k, v| arr << v}
  arr
end

Private Instance Methods

initialize_copy(other) click to toggle source
Calls superclass method
# File lib/thread_safe/cache.rb, line 121
def initialize_copy(other)
  super
  populate_from(other)
end
populate_from(hash) click to toggle source
# File lib/thread_safe/cache.rb, line 126
def populate_from(hash)
  hash.each_pair {|k, v| self[k] = v}
  self
end
validate_options_hash!(options) click to toggle source
# File lib/thread_safe/cache.rb, line 131
def validate_options_hash!(options)
  if (initial_capacity = options[:initial_capacity]) && (!initial_capacity.kind_of?(Fixnum) || initial_capacity < 0)
    raise ArgumentError, ":initial_capacity must be a positive Fixnum"
  end
  if (load_factor = options[:load_factor]) && (!load_factor.kind_of?(Numeric) || load_factor <= 0 || load_factor > 1)
    raise ArgumentError, ":load_factor must be a number between 0 and 1"
  end
end