Sha256: d5b597b148670218997a0da484c51183f786733cadb9fba6353fa4e8962e9d0b

Contents?: true

Size: 761 Bytes

Versions: 32

Compression:

Stored size: 761 Bytes

Contents

module MultiJson
  module OptionsCache
    extend self

    def reset
      @dump_cache = {}
      @load_cache = {}
    end

    def fetch(type, key, &block)
      cache = instance_variable_get("@#{type}_cache")
      cache.key?(key) ? cache[key] : write(cache, key, &block)
    end

    private

    # 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.
    MAX_CACHE_SIZE = 1000

    def write(cache, key)
      cache.clear if cache.length >= MAX_CACHE_SIZE
      cache[key] = yield
    end
  end
end

Version data entries

32 entries across 29 versions & 10 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.0.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
logstash-output-newrelic-1.2.0 vendor/bundle/jruby/2.5.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
logstash-filter-csharp-0.2.1 vendor/bundle/jruby/2.5.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
logstash-filter-csharp-0.2.0 vendor/bundle/jruby/2.5.0/gems/multi_json-1.15.0/lib/multi_json/options_cache.rb
multi_json-1.15.0 lib/multi_json/options_cache.rb
logstash-output-scalyr-0.1.5 vendor/bundle/jruby/2.5.0/gems/multi_json-1.14.1/lib/multi_json/options_cache.rb