Class: Aws::LRUCache Private

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-sdk-core/lru_cache.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A simple thread safe LRU cache

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LRUCache

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of LRUCache.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :max_entries (Integer) — default: 100

    Maximum number of entries

  • :expiration (Integer) — default: nil

    Expiration time in seconds



10
11
12
13
14
15
# 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

Instance Method Details

#[](key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • key (String)

Returns:

  • (Object)


19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • key (String)
  • value (Object)


32
33
34
35
36
37
38
39
40
# 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

#clearObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



51
52
53
54
55
# File 'lib/aws-sdk-core/lru_cache.rb', line 51

def clear
  @mutex.synchronize do
    @entries.clear
  end
end

#key?(key) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • key (String)

Returns:

  • (Boolean)


44
45
46
47
48
49
# 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