Class: LaunchDarkly::SimpleLRUCacheSet

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/simple_lru_cache.rb

Overview

A non-thread-safe implementation of a LRU cache set with only add and reset methods. Based on github.com/SamSaffron/lru_redux/blob/master/lib/lru_redux/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ SimpleLRUCacheSet

Returns a new instance of SimpleLRUCacheSet.



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

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

Instance Method Details

#add(value) ⇒ Object

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



13
14
15
16
17
18
19
# File 'lib/ldclient-rb/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



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

def clear
  @values = {}
end