Class: Kilden::FlagCache Private

Inherits:
Object
  • Object
show all
Defined in:
lib/kilden/flag_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.

TTL + LRU cache of /decide responses, keyed by distinct_id (spec ยง8.2: TTL 30s, at most 1000 ids). Ruby's insertion-ordered Hash doubles as the LRU list: delete + reinsert on hit, shift the oldest on overflow.

Constant Summary collapse

TTL =

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

30
MAX_IDS =

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

1000

Instance Method Summary collapse

Constructor Details

#initialize(clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }) ⇒ FlagCache

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



12
13
14
15
16
# File 'lib/kilden/flag_cache.rb', line 12

def initialize(clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
  @clock = clock
  @entries = {}
  @mutex = Mutex.new
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.



37
38
39
# File 'lib/kilden/flag_cache.rb', line 37

def clear
  @mutex.synchronize { @entries.clear }
end

#get(distinct_id) ⇒ 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.



18
19
20
21
22
23
24
25
26
27
# File 'lib/kilden/flag_cache.rb', line 18

def get(distinct_id)
  @mutex.synchronize do
    entry = @entries.delete(distinct_id)
    return nil unless entry
    return nil if @clock.call >= entry[0]

    @entries[distinct_id] = entry
    entry[1]
  end
end

#set(distinct_id, flags) ⇒ 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.



29
30
31
32
33
34
35
# File 'lib/kilden/flag_cache.rb', line 29

def set(distinct_id, flags)
  @mutex.synchronize do
    @entries.delete(distinct_id)
    @entries[distinct_id] = [@clock.call + TTL, flags]
    @entries.shift if @entries.size > MAX_IDS
  end
end