Sha256: b864678cbfccc298445836aa0f23ff82ce03b2a05722d20cf991deff1dbf8281

Contents?: true

Size: 773 Bytes

Versions: 1

Compression:

Stored size: 773 Bytes

Contents

# frozen_string_literal: true

module SpartanAPM
  # Simple string cache. This is used to prevent memory bloat
  # when collecting measures by caching and reusing strings
  # in the enqueued metrics rather than having the same string
  # repeated for each request.
  class StringCache
    def initialize
      @cache = Concurrent::Hash.new
    end

    # Fetch a string from the cache. If it isn't already there,
    # then it will be frozen and stored in the cache so the same
    # object can be returned by subsequent calls for a matching string.
    def fetch(value)
      return nil if value.nil?
      value = value.to_s
      cached = @cache[value]
      unless cached
        cached = -value
        @cache[cached] = cached
      end
      cached
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spartan_apm-0.0.0.rc1 lib/spartan_apm/string_cache.rb