class MultiJson::OptionsCache::Store
def fetch(key, &block)
def fetch(key, &block) @mutex.synchronize do return @cache[key] if @cache.key?(key) end value = yield @mutex.synchronize do if @cache.key?(key) # We ran into a race condition, keep the existing value @cache[key] else @cache.clear if @cache.size >= MAX_CACHE_SIZE @cache[key] = value end end end
def initialize
def initialize @cache = {} @mutex = Mutex.new end
def reset
def reset @mutex.synchronize do @cache = {} end end