class ElasticAPM::Util::LruCache

@api private

Public Class Methods

new(max_size = 512, &block) click to toggle source
# File lib/elastic_apm/util/lru_cache.rb, line 7
def initialize(max_size = 512, &block)
  @max_size = max_size
  @data = Hash.new(&block)
  @mutex = Mutex.new
end

Public Instance Methods

[](key) click to toggle source
# File lib/elastic_apm/util/lru_cache.rb, line 13
def [](key)
  @mutex.synchronize do
    val = @data[key]
    return unless val
    add(key, val)
    val
  end
end
[]=(key, val) click to toggle source
# File lib/elastic_apm/util/lru_cache.rb, line 22
def []=(key, val)
  @mutex.synchronize do
    add(key, val)
  end
end
length() click to toggle source
# File lib/elastic_apm/util/lru_cache.rb, line 28
def length
  @data.length
end
to_a() click to toggle source
# File lib/elastic_apm/util/lru_cache.rb, line 32
def to_a
  @data.to_a
end

Private Instance Methods

add(key, val) click to toggle source
# File lib/elastic_apm/util/lru_cache.rb, line 38
def add(key, val)
  @data.delete(key)
  @data[key] = val

  return unless @data.length > @max_size

  @data.delete(@data.first[0])
end