class MultiJson::OptionsCache::Store
Constants
- MAX_CACHE_SIZE
-
Normally
MultiJson
is used with a few option sets for both dump/load methods. When options are generated dynamically though, every call would cause a cache miss and the cache would grow indefinitely. To prevent this, we just reset the cache every time the number of keys outgrows 1000.
Public Class Methods
Source
# File lib/multi_json/options_cache.rb, line 12 def initialize @cache = {} @mutex = Mutex.new end
Public Instance Methods
Source
# File lib/multi_json/options_cache.rb, line 23 def fetch(key, &) @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
Source
# File lib/multi_json/options_cache.rb, line 17 def reset @mutex.synchronize do @cache = {} end end