FIX: do not store key tracking last seen time indefinitely

UserStat has some special logic to keep adding time read if repeat calls
are made in intervals less than 100 seconds. This is called regularly
when we update read timings on a topic.

We only need to cache this key in redis for 100 seconds, however previously
we would keep it forever, 1 key per user. This has potential of bloating
a very large amount of keys for no longer active users in redis.
This commit is contained in:
Sam 2018-12-03 08:35:09 +11:00
parent 26e9b628b8
commit 236c755d62
2 changed files with 10 additions and 1 deletions

View File

@ -101,7 +101,7 @@ class UserStat < ActiveRecord::Base
end
def self.cache_last_seen(id, val)
$redis.set(last_seen_key(id), val)
$redis.setex(last_seen_key(id), MAX_TIME_READ_DIFF, val)
end
protected

View File

@ -76,6 +76,15 @@ describe UserStat do
let(:user) { Fabricate(:user) }
let(:stat) { user.user_stat }
it 'always expires redis key' do
# this tests implementation which is not 100% ideal
# that said, redis key leaks are not good
stat.update_time_read!
ttl = $redis.ttl(UserStat.last_seen_key(user.id))
expect(ttl).to be > 0
expect(ttl).to be <= UserStat::MAX_TIME_READ_DIFF
end
it 'makes no changes if nothing is cached' do
$redis.del(UserStat.last_seen_key(user.id))
stat.update_time_read!