ConcurrentCacheBackend
# 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
# File lib/thread_safe/cache.rb, line 100 def each_key each_pair {|k, v| yield k} end
# File lib/thread_safe/cache.rb, line 104 def each_value each_pair {|k, v| yield v} end
# File lib/thread_safe/cache.rb, line 114 def empty? each_pair {|k, v| return false} true end
# 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
# 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
# File lib/thread_safe/cache.rb, line 108 def key(value) each_pair {|k, v| return k if v == value} nil end
# File lib/thread_safe/cache.rb, line 88 def keys arr = [] each_pair {|k, v| arr << k} arr end
# 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
# File lib/thread_safe/cache.rb, line 132 def marshal_load(hash) initialize populate_from(hash) end
# 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
# File lib/thread_safe/cache.rb, line 119 def size count = 0 each_pair {|k, v| count += 1} count end
Generated with the Darkfish Rdoc Generator 2.