diff --git a/app/models/user_stat.rb b/app/models/user_stat.rb index 65f48d62dc3..c4806af20b7 100644 --- a/app/models/user_stat.rb +++ b/app/models/user_stat.rb @@ -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 diff --git a/spec/models/user_stat_spec.rb b/spec/models/user_stat_spec.rb index 43e73819a14..be844c015d1 100644 --- a/spec/models/user_stat_spec.rb +++ b/spec/models/user_stat_spec.rb @@ -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!