class Aws::LRUCache

@api private A simple thread safe LRU cache

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options @option options [Integer] :max_entries (100) Maximum number of entries @option options [Integer] :expiration (nil) Expiration time in seconds

# File lib/aws-sdk-core/lru_cache.rb, line 10
def initialize(options = {})
  @max_entries = options[:max_entries] || 100
  @expiration = options[:expiration]
  @entries = {}
  @mutex = Mutex.new
end

Public Instance Methods

[](key) click to toggle source

@param [String] key @return [Object]

# File lib/aws-sdk-core/lru_cache.rb, line 19
def [](key)
  @mutex.synchronize do
    value = @entries[key]
    if value
      @entries.delete(key)
      @entries[key] = value unless value.expired?
    end
    @entries[key]&.value
  end
end
[]=(key, value) click to toggle source

@param [String] key @param [Object] value

# File lib/aws-sdk-core/lru_cache.rb, line 32
def []=(key, value)
  @mutex.synchronize do
    @entries.shift unless @entries.size < @max_entries
    # delete old value if exists
    @entries.delete(key)
    @entries[key] = Entry.new(value: value, expiration: @expiration)
    @entries[key].value
  end
end
clear() click to toggle source
# File lib/aws-sdk-core/lru_cache.rb, line 51
def clear
  @mutex.synchronize do
    @entries.clear
  end
end
key?(key) click to toggle source

@param [String] key @return [Boolean]

# File lib/aws-sdk-core/lru_cache.rb, line 44
def key?(key)
  @mutex.synchronize do
    @entries.delete(key) if @entries.key?(key) && @entries[key].expired?
    @entries.key?(key)
  end
end