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:
parent
26e9b628b8
commit
236c755d62
|
@ -101,7 +101,7 @@ class UserStat < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.cache_last_seen(id, val)
|
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
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
@ -76,6 +76,15 @@ describe UserStat do
|
||||||
let(:user) { Fabricate(:user) }
|
let(:user) { Fabricate(:user) }
|
||||||
let(:stat) { user.user_stat }
|
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
|
it 'makes no changes if nothing is cached' do
|
||||||
$redis.del(UserStat.last_seen_key(user.id))
|
$redis.del(UserStat.last_seen_key(user.id))
|
||||||
stat.update_time_read!
|
stat.update_time_read!
|
||||||
|
|
Loading…
Reference in New Issue