Class: ThreadSafe::Cache
- Inherits:
-
ConcurrentCacheBackend
- Object
- ThreadSafe::Cache
- Defined in:
- lib/skylight/vendor/thread_safe.rb
Constant Summary collapse
- KEY_ERROR =
there is no KeyError in 1.8 mode
defined?(KeyError) ? KeyError : IndexError
Instance Method Summary collapse
- #[](key) ⇒ Object
- #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.
- #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.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/skylight/vendor/thread_safe.rb', line 12 def initialize( = nil, &block) if .kind_of?(::Hash) () else = nil end super() @default_proc = block end |
Instance Method Details
#[](key) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/skylight/vendor/thread_safe.rb', line 23 def [](key) if value = super value elsif @default_proc && !key?(key) @default_proc.call(self, key) else value end end |
#each_key ⇒ Object
73 74 75 |
# File 'lib/skylight/vendor/thread_safe.rb', line 73 def each_key each_pair {|k, v| yield k} end |
#each_value ⇒ Object
77 78 79 |
# File 'lib/skylight/vendor/thread_safe.rb', line 77 def each_value each_pair {|k, v| yield v} end |
#empty? ⇒ Boolean
81 82 83 84 |
# File 'lib/skylight/vendor/thread_safe.rb', line 81 def empty? each_pair {|k, v| return false} true end |
#fetch(key, default_value = NULL) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/skylight/vendor/thread_safe.rb', line 33 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 |
#keys ⇒ Object
61 62 63 64 65 |
# File 'lib/skylight/vendor/thread_safe.rb', line 61 def keys arr = [] each_pair {|k, v| arr << k} arr end |
#marshal_dump ⇒ Object
92 93 94 95 96 97 |
# File 'lib/skylight/vendor/thread_safe.rb', line 92 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
99 100 101 102 |
# File 'lib/skylight/vendor/thread_safe.rb', line 99 def marshal_load(hash) initialize populate_from(hash) end |
#put_if_absent(key, value) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/skylight/vendor/thread_safe.rb', line 45 def put_if_absent(key, value) computed = false result = compute_if_absent(key) do computed = true value end computed ? nil : result end |
#size ⇒ Object
86 87 88 89 90 |
# File 'lib/skylight/vendor/thread_safe.rb', line 86 def size count = 0 each_pair {|k, v| count += 1} count end |
#value?(value) ⇒ Boolean
54 55 56 57 58 59 |
# File 'lib/skylight/vendor/thread_safe.rb', line 54 def value?(value) each_value do |v| return true if value.equal?(v) end false end |
#values ⇒ Object
67 68 69 70 71 |
# File 'lib/skylight/vendor/thread_safe.rb', line 67 def values arr = [] each_pair {|k, v| arr << v} arr end |