Class: ThreadSafe::Cache
- Inherits:
-
ConcurrentCacheBackend
- Object
- ThreadSafe::Cache
- 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
- #[](key) ⇒ Object (also: #get)
- #each_key ⇒ Object
- #each_value ⇒ Object
- #empty? ⇒ Boolean
- #fetch(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.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/thread_safe/cache.rb', line 26 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
37 38 39 40 41 42 43 44 45 |
# File 'lib/thread_safe/cache.rb', line 37 def [](key) if value = super value elsif @default_proc && !key?(key) @default_proc.call(self, key) else value end end |
#each_key ⇒ Object
90 91 92 |
# File 'lib/thread_safe/cache.rb', line 90 def each_key each_pair {|k, v| yield k} end |
#each_value ⇒ Object
94 95 96 |
# File 'lib/thread_safe/cache.rb', line 94 def each_value each_pair {|k, v| yield v} end |
#empty? ⇒ Boolean
104 105 106 107 |
# File 'lib/thread_safe/cache.rb', line 104 def empty? each_pair {|k, v| return false} true end |
#fetch(key, default_value = NULL) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/thread_safe/cache.rb', line 50 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 |
#key(value) ⇒ Object Also known as: index
98 99 100 101 |
# File 'lib/thread_safe/cache.rb', line 98 def key(value) each_pair {|k, v| return k if v == value} nil end |
#keys ⇒ Object
78 79 80 81 82 |
# File 'lib/thread_safe/cache.rb', line 78 def keys arr = [] each_pair {|k, v| arr << k} arr end |
#marshal_dump ⇒ Object
115 116 117 118 119 120 |
# File 'lib/thread_safe/cache.rb', line 115 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
122 123 124 125 |
# File 'lib/thread_safe/cache.rb', line 122 def marshal_load(hash) initialize populate_from(hash) end |
#put_if_absent(key, value) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/thread_safe/cache.rb', line 62 def put_if_absent(key, value) computed = false result = compute_if_absent(key) do computed = true value end computed ? nil : result end |
#size ⇒ Object
109 110 111 112 113 |
# File 'lib/thread_safe/cache.rb', line 109 def size count = 0 each_pair {|k, v| count += 1} count end |
#value?(value) ⇒ Boolean
71 72 73 74 75 76 |
# File 'lib/thread_safe/cache.rb', line 71 def value?(value) each_value do |v| return true if value.equal?(v) end false end |
#values ⇒ Object
84 85 86 87 88 |
# File 'lib/thread_safe/cache.rb', line 84 def values arr = [] each_pair {|k, v| arr << v} arr end |