class Aws::LRUCache
@api private A simple thread safe LRU cache
Public Class Methods
Source
# 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
@param [Hash] options @option options [Integer] :max_entries (100) Maximum number of entries @option options [Integer] :expiration (nil) Expiration time in seconds
Public Instance Methods
Source
# 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
@param [String] key @return [Object]
Source
# 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
@param [String] key @param [Object] value
Source
# File lib/aws-sdk-core/lru_cache.rb, line 51 def clear @mutex.synchronize do @entries.clear end end
Source
# 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
@param [String] key @return [Boolean]