Class: ThreadSafe::Cache

Inherits:
ConcurrentCacheBackend
  • Object
show all
Defined in:
lib/thread_safe/cache.rb

Constant Summary collapse

KEY_ERROR =

there is no KeyError in 1.8 mode

defined?(KeyError) ? KeyError : IndexError

Instance Method Summary collapse

Constructor Details

#initialize(options = nil, &block) ⇒ Cache

Returns a new instance of Cache.



26
27
28
29
30
31
32
33
34
35
# File 'lib/thread_safe/cache.rb', line 26

def initialize(options = nil, &block)
  if options.kind_of?(::Hash)
    validate_options_hash!(options)
  else
    options = nil
  end

  super(options)
  @default_proc = block
end

Instance Method Details

#[](key) ⇒ Object Also known as: get



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/thread_safe/cache.rb', line 37

def [](key)
  if value = super # non-falsy value is an existing mapping, return it right away
    value
  # re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
  # a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
  # would be returned)
  # note: nil == value check is not technically necessary
  elsif @default_proc && nil == value && NULL == (value = get_or_default(key, NULL))
    @default_proc.call(self, key)
  else
    value
  end
end

#each_keyObject



100
101
102
# File 'lib/thread_safe/cache.rb', line 100

def each_key
  each_pair {|k, v| yield k}
end

#each_valueObject



104
105
106
# File 'lib/thread_safe/cache.rb', line 104

def each_value
  each_pair {|k, v| yield v}
end

#empty?Boolean

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/thread_safe/cache.rb', line 114

def empty?
  each_pair {|k, v| return false}
  true
end

#fetch(key, default_value = NULL) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/thread_safe/cache.rb', line 54

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_fetch_no_key
  end
end

#fetch_or_store(key, default_value = NULL) ⇒ Object



66
67
68
69
70
# File 'lib/thread_safe/cache.rb', line 66

def fetch_or_store(key, default_value = NULL)
  fetch(key) do
    put(key, block_given? ? yield(key) : (NULL == default_value ? raise_fetch_no_key : default_value))
  end
end

#key(value) ⇒ Object Also known as: index



108
109
110
111
# File 'lib/thread_safe/cache.rb', line 108

def key(value)
  each_pair {|k, v| return k if v == value}
  nil
end

#keysObject



88
89
90
91
92
# File 'lib/thread_safe/cache.rb', line 88

def keys
  arr = []
  each_pair {|k, v| arr << k}
  arr
end

#marshal_dumpObject

Raises:

  • (TypeError)


125
126
127
128
129
130
# File 'lib/thread_safe/cache.rb', line 125

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) ⇒ Object



132
133
134
135
# File 'lib/thread_safe/cache.rb', line 132

def marshal_load(hash)
  initialize
  populate_from(hash)
end

#put_if_absent(key, value) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/thread_safe/cache.rb', line 72

def put_if_absent(key, value)
  computed = false
  result = compute_if_absent(key) do
    computed = true
    value
  end
  computed ? nil : result
end

#sizeObject



119
120
121
122
123
# File 'lib/thread_safe/cache.rb', line 119

def size
  count = 0
  each_pair {|k, v| count += 1}
  count
end

#value?(value) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/thread_safe/cache.rb', line 81

def value?(value)
  each_value do |v|
    return true if value.equal?(v)
  end
  false
end

#valuesObject



94
95
96
97
98
# File 'lib/thread_safe/cache.rb', line 94

def values
  arr = []
  each_pair {|k, v| arr << v}
  arr
end