Class: ThreadSafe::Cache
- Inherits:
-
ConcurrentCacheBackend
- Object
- ThreadSafe::Cache
- Defined in:
- lib/thread_safe/cache.rb
Instance Method Summary collapse
- #[](key) ⇒ Object (also: #get)
- #each_key ⇒ Object
- #each_value ⇒ Object
- #empty? ⇒ Boolean
- #fetch(key, default_value = NULL) ⇒ Object
- #fetch_or_store(key, default_value = NULL) ⇒ Object
-
#initialize(options = nil, &block) ⇒ Cache
constructor
A new instance of Cache.
- #key(value) ⇒ Object (also: #index)
- #keys ⇒ Object
- #marshal_dump ⇒ Object
- #marshal_load(hash) ⇒ Object
- #put_if_absent(key, value) ⇒ Object
- #size ⇒ Object
- #value?(value) ⇒ Boolean
- #values ⇒ Object
Constructor Details
#initialize(options = nil, &block) ⇒ Cache
Returns a new instance of Cache.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/thread_safe/cache.rb', line 24 def initialize( = nil, &block) if .kind_of?(::Hash) () else = nil end super() @default_proc = block end |
Instance Method Details
#[](key) ⇒ Object Also known as: get
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/thread_safe/cache.rb', line 35 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_key ⇒ Object
98 99 100 |
# File 'lib/thread_safe/cache.rb', line 98 def each_key each_pair {|k, v| yield k} end |
#each_value ⇒ Object
102 103 104 |
# File 'lib/thread_safe/cache.rb', line 102 def each_value each_pair {|k, v| yield v} end |
#empty? ⇒ Boolean
112 113 114 115 |
# File 'lib/thread_safe/cache.rb', line 112 def empty? each_pair {|k, v| return false} true end |
#fetch(key, default_value = NULL) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/thread_safe/cache.rb', line 52 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
64 65 66 67 68 |
# File 'lib/thread_safe/cache.rb', line 64 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
106 107 108 109 |
# File 'lib/thread_safe/cache.rb', line 106 def key(value) each_pair {|k, v| return k if v == value} nil end |
#keys ⇒ Object
86 87 88 89 90 |
# File 'lib/thread_safe/cache.rb', line 86 def keys arr = [] each_pair {|k, v| arr << k} arr end |
#marshal_dump ⇒ Object
123 124 125 126 127 128 |
# File 'lib/thread_safe/cache.rb', line 123 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
130 131 132 133 |
# File 'lib/thread_safe/cache.rb', line 130 def marshal_load(hash) initialize populate_from(hash) end |
#put_if_absent(key, value) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/thread_safe/cache.rb', line 70 def put_if_absent(key, value) computed = false result = compute_if_absent(key) do computed = true value end computed ? nil : result end |
#size ⇒ Object
117 118 119 120 121 |
# File 'lib/thread_safe/cache.rb', line 117 def size count = 0 each_pair {|k, v| count += 1} count end |
#value?(value) ⇒ Boolean
79 80 81 82 83 84 |
# File 'lib/thread_safe/cache.rb', line 79 def value?(value) each_value do |v| return true if value.equal?(v) end false end |
#values ⇒ Object
92 93 94 95 96 |
# File 'lib/thread_safe/cache.rb', line 92 def values arr = [] each_pair {|k, v| arr << v} arr end |