Class: LaunchDarkly::Impl::SimpleLRUCacheSet Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/simple_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 non-thread-safe implementation of a LRU cache set with only add and reset methods. Based on https://github.com/SamSaffron/lru_redux/blob/master/lib/lru_redux/cache.rb

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ SimpleLRUCacheSet

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

Since:

  • 5.5.0



7
8
9
10
# File 'lib/ldclient-rb/impl/simple_lru_cache.rb', line 7

def initialize(capacity)
  @values = {}
  @capacity = capacity
end

Instance Method Details

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

Adds a value to the cache or marks it recent if it was already there. Returns true if already there.

Since:

  • 5.5.0



13
14
15
16
17
18
19
# File 'lib/ldclient-rb/impl/simple_lru_cache.rb', line 13

def add(value)
  found = true
  @values.delete(value) { found = false }
  @values[value] = true
  @values.shift if @values.length > @capacity
  found
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.

Since:

  • 5.5.0



21
22
23
# File 'lib/ldclient-rb/impl/simple_lru_cache.rb', line 21

def clear
  @values = {}
end