module MultiJson::OptionsCache

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 Instance Methods

fetch(type, key) click to toggle source
# File lib/multi_json/options_cache.rb, line 10
def fetch(type, key)
  cache = instance_variable_get("@#{type}_cache")
  cache.key?(key) ? cache[key] : write(cache, key, &Proc.new)
end
reset() click to toggle source
# File lib/multi_json/options_cache.rb, line 5
def reset
  @dump_cache = {}
  @load_cache = {}
end

Private Instance Methods

write(cache, key) { || ... } click to toggle source
# File lib/multi_json/options_cache.rb, line 24
def write(cache, key)
  cache.clear if cache.length >= MAX_CACHE_SIZE
  cache[key] = yield
end