Module: Weathercock::Scorable
- Defined in:
- lib/weathercock/scorable.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- WEATHERCOCK_BUCKET_TTLS =
{ hours: 3 * 24 * 3600, days: 3 * 30 * 86400, months: 3 * 12 * 30 * 86400 }.freeze
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(base) ⇒ Object
14 15 16 |
# File 'lib/weathercock/scorable.rb', line 14 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#hit(event, increment: 1) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/weathercock/scorable.rb', line 76 def hit(event, increment: 1) now = Time.now redis = Weathercock.config.redis base = self.class.weathercock_base_key(event) redis.pipelined do |p| p.call("ZINCRBY", "#{base}:total", increment, id.to_s) WEATHERCOCK_BUCKET_TTLS.each do |type, ttl| key = self.class.send(:weathercock_bucket_key, base, type, now) p.call("ZINCRBY", key, increment, id.to_s) p.call("EXPIRE", key, ttl) end end end |
#hit_count(event, **window) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/weathercock/scorable.rb', line 91 def hit_count(event, **window) if window.empty? score = Weathercock.config.redis.call("ZSCORE", "#{self.class.weathercock_base_key(event)}:total", id.to_s) return score ? score.to_i : 0 end dest = self.class.send(:weathercock_union, event, window) score = Weathercock.config.redis.call("ZSCORE", dest, id.to_s) score ? score.to_i : 0 end |