Class: UserPattern::ThresholdCache
- Inherits:
-
Object
- Object
- UserPattern::ThresholdCache
- 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
- #all_limits ⇒ Object
-
#initialize ⇒ ThresholdCache
constructor
A new instance of ThresholdCache.
- #known_endpoint?(model_type, endpoint) ⇒ Boolean
- #limits_for(model_type, endpoint) ⇒ Object
- #refresh! ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize ⇒ ThresholdCache
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_limits ⇒ Object
28 29 30 |
# File 'lib/userpattern/threshold_cache.rb', line 28 def all_limits @limits.dup end |
#known_endpoint?(model_type, endpoint) ⇒ 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 |
#shutdown ⇒ Object
37 38 39 |
# File 'lib/userpattern/threshold_cache.rb', line 37 def shutdown @timer&.shutdown end |