2019-07-02 08:10:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityTracker
|
|
|
|
EXPIRE_AFTER = 90.days.seconds
|
|
|
|
|
|
|
|
class << self
|
|
|
|
include Redisable
|
|
|
|
|
|
|
|
def increment(prefix)
|
|
|
|
key = [prefix, current_week].join(':')
|
|
|
|
|
2021-01-17 22:36:20 +00:00
|
|
|
redis.with do |conn|
|
|
|
|
conn.incrby(key, 1)
|
|
|
|
conn.expire(key, EXPIRE_AFTER)
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def record(prefix, value)
|
|
|
|
key = [prefix, current_week].join(':')
|
|
|
|
|
2021-01-17 22:36:20 +00:00
|
|
|
redis.with do |conn|
|
|
|
|
conn.pfadd(key, value)
|
|
|
|
conn.expire(key, EXPIRE_AFTER)
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def current_week
|
|
|
|
Time.zone.today.cweek
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|