mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 03:16:41 +08:00
DEV: clear last seen cache consistently
Previously in some cases the test suite could fail due to a bad entry in redis from previous tests This ensures the correct cache is expired when needed Additionally improves performance of the redis check
This commit is contained in:
parent
8ca8a7edba
commit
b31da92ede
|
@ -727,13 +727,26 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def update_last_seen!(now = Time.zone.now)
|
||||
def last_seen_redis_key(now)
|
||||
now_date = now.to_date
|
||||
# Only update last seen once every minute
|
||||
redis_key = "user:#{id}:#{now_date}"
|
||||
return unless Discourse.redis.setnx(redis_key, "1")
|
||||
"user:#{id}:#{now_date}"
|
||||
end
|
||||
|
||||
def clear_last_seen_cache!(now = Time.zone.now)
|
||||
Discourse.redis.del(last_seen_redis_key(now))
|
||||
end
|
||||
|
||||
def update_last_seen!(now = Time.zone.now)
|
||||
redis_key = last_seen_redis_key(now)
|
||||
|
||||
if SiteSetting.active_user_rate_limit_secs > 0
|
||||
return if !Discourse.redis.set(
|
||||
redis_key, "1",
|
||||
nx: true,
|
||||
ex: SiteSetting.active_user_rate_limit_secs
|
||||
)
|
||||
end
|
||||
|
||||
Discourse.redis.expire(redis_key, SiteSetting.active_user_rate_limit_secs)
|
||||
update_previous_visit(now)
|
||||
# using update_column to avoid the AR transaction
|
||||
update_column(:last_seen_at, now)
|
||||
|
|
|
@ -244,13 +244,16 @@ describe Auth::DefaultCurrentUserProvider do
|
|||
cookies["_t"][:value]
|
||||
end
|
||||
|
||||
before do
|
||||
@orig = freeze_time
|
||||
user.clear_last_seen_cache!(@orig)
|
||||
end
|
||||
|
||||
after do
|
||||
Discourse.redis.flushdb
|
||||
user.clear_last_seen_cache!(@orig)
|
||||
end
|
||||
|
||||
it "should not update last seen for suspended users" do
|
||||
freeze_time
|
||||
|
||||
provider2 = provider("/", "HTTP_COOKIE" => "_t=#{unhashed_token}")
|
||||
u = provider2.current_user
|
||||
u.reload
|
||||
|
@ -262,7 +265,8 @@ describe Auth::DefaultCurrentUserProvider do
|
|||
u.suspended_till = 1.year.from_now
|
||||
u.save!
|
||||
|
||||
Discourse.redis.del("user:#{user.id}:#{Time.now.to_date}")
|
||||
u.clear_last_seen_cache!
|
||||
|
||||
provider2 = provider("/", "HTTP_COOKIE" => "_t=#{unhashed_token}")
|
||||
expect(provider2.current_user).to eq(nil)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user