Class: Skylight::Util::LruCache Private

Inherits:
Object
  • Object
show all
Defined in:
lib/skylight/util/lru_cache.rb

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.

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ 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.



5
6
7
8
# File 'lib/skylight/util/lru_cache.rb', line 5

def initialize(max_size)
  @max_size = max_size
  @data = {}
end

Instance Method Details

#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.



31
32
33
# File 'lib/skylight/util/lru_cache.rb', line 31

def clear
  @data.clear
end

#fetch(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.

Individual hash operations here are atomic in MRI.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/skylight/util/lru_cache.rb', line 18

def fetch(key)
  found = true
  value = @data.delete(key) { found = false }

  value = yield if !found && block_given?

  @data[key] = value if value

  @data.shift if !found && value && @data.length > @max_size

  value
end

#max_size=(size) ⇒ 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.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
# File 'lib/skylight/util/lru_cache.rb', line 10

def max_size=(size)
  raise ArgumentError, :max_size if @max_size < 1

  @max_size = size
  @data.shift while @data.size > @max_size
end