Class: Fluent::Plugin::KubeletMetadata::ThreadsafeLruCache

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/filter_kubelet_metadata.rb

Overview

  • no operations on all values to be fast

  • cannot store nil as value

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ThreadsafeLruCache

Returns a new instance of ThreadsafeLruCache.



30
31
32
33
34
# File 'lib/fluent/plugin/filter_kubelet_metadata.rb', line 30

def initialize(size)
  @size = size
  @data = {}
  @mutex = Mutex.new
end

Instance Method Details

#[](key) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/fluent/plugin/filter_kubelet_metadata.rb', line 36

def [](key)
  @mutex.synchronize do
    value = @data.delete(key) # always remove ... later add it back if necessary
    return if value.nil? # miss
    @data[key] = value # mark as recently used
  end
end

#[]=(key, value) ⇒ Object



44
45
46
47
48
49
# File 'lib/fluent/plugin/filter_kubelet_metadata.rb', line 44

def []=(key, value)
  @mutex.synchronize do
    @data.delete @data.first[0] if @data.size == @size # make room
    @data[key] = value
  end
end