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



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

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.



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

def add(value)
  found = true
  @values.delete(value) { found = false }
  @values[value] = true
  @values.shift if @values.length > @capacity
  found
end

#clearObject



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

def clear
  @values = {}
end