Class: MultiJson::OptionsCache::Store Private
- Inherits:
-
Object
- Object
- MultiJson::OptionsCache::Store
- Defined in:
- lib/multi_json/options_cache.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Thread-safe cache store using double-checked locking pattern
Constant Summary collapse
- NOT_FOUND =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Sentinel value to detect cache misses (unique object identity)
Object.new
Instance Method Summary collapse
-
#fetch(key, default = nil) { ... } ⇒ Object
private
Fetch a value from cache or compute it.
-
#initialize ⇒ Store
constructor
private
Create a new cache store.
-
#reset ⇒ void
private
Clear all cached entries.
Constructor Details
#initialize ⇒ Store
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new cache store
24 25 26 27 |
# File 'lib/multi_json/options_cache.rb', line 24 def initialize @cache = {} @mutex = Mutex.new end |
Instance Method Details
#fetch(key, default = nil) { ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fetch a value from cache or compute it
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/multi_json/options_cache.rb', line 44 def fetch(key, default = nil) # Fast path: check cache without lock (safe for reads) value = @cache.fetch(key, NOT_FOUND) return value unless value.equal?(NOT_FOUND) # Slow path: acquire lock and compute value @mutex.synchronize do @cache.fetch(key) { block_given? ? store(key, yield) : default } end end |
#reset ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Clear all cached entries
33 34 35 |
# File 'lib/multi_json/options_cache.rb', line 33 def reset @mutex.synchronize { @cache.clear } end |