From 8cab7b0dcc232bcf7c5fa5bb6a45e9c61aef48cd Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Mon, 27 Nov 2023 22:30:10 +0100 Subject: [PATCH] Cache ban state --- app/models/user/ban_methods.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/models/user/ban_methods.rb b/app/models/user/ban_methods.rb index 07826b1b..2fab74a3 100644 --- a/app/models/user/ban_methods.rb +++ b/app/models/user/ban_methods.rb @@ -6,14 +6,17 @@ module User::BanMethods end def banned? - bans.current.count.positive? + Rails.cache.fetch("#{cache_key}/banned") do + bans.current.count.positive? + end end def unban bans.current.update( # -1s to account for flakyness with timings in tests - expires_at: DateTime.now.utc - 1.second + expires_at: DateTime.now.utc - 1.second, ) + Rails.cache.delete("#{cache_key}/banned") end # Bans a user. @@ -24,8 +27,9 @@ module User::BanMethods ::UserBan.create!( user: self, expires_at: expiry, - banned_by: banned_by, - reason: reason + banned_by:, + reason:, ) + Rails.cache.delete("#{cache_key}/banned") end end