class MultiJson::OptionsCache::Store
Thread-safe cache store using double-checked locking pattern
@api private
Constants
- NOT_FOUND
Sentinel value to detect cache misses (unique object identity)
Public Class Methods
new()
click to toggle source
Create a new cache store
@api private @return [Store] new store instance
# File lib/multi_json/options_cache.rb, line 24 def initialize @cache = {} @mutex = Mutex.new end
Public Instance Methods
fetch(key, default = nil) { || ... }
click to toggle source
Fetch a value from cache or compute it
@api private @param key [Object] cache key @param default [Object] default value if key not found @yield block to compute value if not cached @return [Object] cached or computed value
# 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()
click to toggle source
Clear all cached entries
@api private @return [void]
# File lib/multi_json/options_cache.rb, line 33 def reset @mutex.synchronize { @cache.clear } end
Private Instance Methods
store(key, value)
click to toggle source
Stores a value in the cache with LRU eviction
@api private @param key [Object] cache key @param value [Object] value to store @return [Object] the stored value
# File lib/multi_json/options_cache.rb, line 63 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