class MultiJson::OptionsCache::Store
@api private
Thread-safe cache store using double-checked locking pattern
def fetch(key, default = nil)
-
(Object)- cached or computed value
Other tags:
- Yield: - block to compute value if not cached
Parameters:
-
default(Object) -- default value if key not found -
key(Object) -- cache key
Other tags:
- Api: - private
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
def initialize
-
(Store)- new store instance
Other tags:
- Api: - private
def initialize @cache = {} @mutex = Mutex.new end
def reset
-
(void)-
Other tags:
- Api: - private
def reset @mutex.synchronize { @cache.clear } end
def store(key, value)
-
(Object)- the stored value
Parameters:
-
value(Object) -- value to store -
key(Object) -- cache key
Other tags:
- Api: - private
def store(key, value) # Double-check in case another thread computed while we waited @cache.fetch(key) do # Evict oldest entry if at capacity (Hash maintains insertion order) @cache.shift if @cache.size >= MAX_CACHE_SIZE @cache[key] = value end end