activity_tracker.rb 506 B

1234567891011121314151617181920212223242526272829
  1. # frozen_string_literal: true
  2. class ActivityTracker
  3. EXPIRE_AFTER = 6.months.seconds
  4. class << self
  5. include Redisable
  6. def increment(prefix)
  7. key = [prefix, current_week].join(':')
  8. redis.incrby(key, 1)
  9. redis.expire(key, EXPIRE_AFTER)
  10. end
  11. def record(prefix, value)
  12. key = [prefix, current_week].join(':')
  13. redis.pfadd(key, value)
  14. redis.expire(key, EXPIRE_AFTER)
  15. end
  16. private
  17. def current_week
  18. Time.zone.today.cweek
  19. end
  20. end
  21. end