Class: UserPattern::ThresholdCache

Inherits:
Object
  • Object
show all
Defined in:
lib/userpattern/threshold_cache.rb

Overview

Periodically loads observed max frequencies from the DB and builds an in-memory lookup of limits (max * multiplier) per (model_type, endpoint).

A Hash is used rather than a Set because we need associated limit values per key — Hash#key? is already O(1), same as Set#include?.

Instance Method Summary collapse

Constructor Details

#initializeThresholdCache

Returns a new instance of ThresholdCache.



13
14
15
16
17
18
# File 'lib/userpattern/threshold_cache.rb', line 13

def initialize
  @limits = {}
  @mutex = Mutex.new
  safe_refresh
  start_refresh_timer
end

Instance Method Details

#all_limitsObject



28
29
30
# File 'lib/userpattern/threshold_cache.rb', line 28

def all_limits
  @limits.dup
end

#known_endpoint?(model_type, endpoint) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/userpattern/threshold_cache.rb', line 24

def known_endpoint?(model_type, endpoint)
  @limits.key?([model_type, endpoint])
end

#limits_for(model_type, endpoint) ⇒ Object



20
21
22
# File 'lib/userpattern/threshold_cache.rb', line 20

def limits_for(model_type, endpoint)
  @limits[[model_type, endpoint]]
end

#refresh!Object



32
33
34
35
# File 'lib/userpattern/threshold_cache.rb', line 32

def refresh!
  new_limits = build_limits
  @mutex.synchronize { @limits = new_limits }
end

#shutdownObject



37
38
39
# File 'lib/userpattern/threshold_cache.rb', line 37

def shutdown
  @timer&.shutdown
end