A platform for community discussion. Free, open, simple.
FIX: Cache rewind reports with user ID not username (#37872)
Martin Brennanb9fb6dca9e49
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rbindex d018c0adad3..0361f25acc5 100644--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb@@ -47,10 +47,10 @@ module DiscourseRewindreturn if !report_classreport_name = report_class.name.demodulize- report = load_single_report_from_cache(for_user.username, year, report_name)+ report = load_single_report_from_cache(for_user.id, year, report_name)if !reportreport = report_class.call(date:, user: for_user)- cache_single_report(for_user.username, year, report_name, report.as_json)+ cache_single_report(for_user.id, year, report_name, report.as_json)endreportenddiff --git a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rbindex 31d986549b8..bba3e1d1a70 100644--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb@@ -61,7 +61,7 @@ module DiscourseRewindenddef fetch_reports(date:, for_user:, year:)- reports = load_reports_from_cache(for_user.username, year)+ reports = load_reports_from_cache(for_user.id, year)if !reportsreports =@@ -72,7 +72,7 @@ module DiscourseRewindrescue StandardErrornilend- cache_reports(for_user.username, year, reports)+ cache_reports(for_user.id, year, reports)endreportsdiff --git a/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rb b/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rbindex 85893abbb27..84fb3ee2d92 100644--- a/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rb+++ b/plugins/discourse-rewind/lib/discourse_rewind/fetch_reports_helper.rb@@ -7,34 +7,34 @@ module DiscourseRewindREWIND_REPORT_VERSION = "2"CACHE_DURATION = Rails.env.development? ? 10.seconds : 3.days- def cache_key(username, year)- "rewind:#{username}:#{year}:v#{REWIND_REPORT_VERSION}"+ def cache_key(user_id, year)+ "rewind:#{user_id}:#{year}:v#{REWIND_REPORT_VERSION}"end- def single_report_cache_key(username, year, name)- "rewind:#{username}:#{year}:#{name}:v#{REWIND_REPORT_VERSION}"+ def single_report_cache_key(user_id, year, name)+ "rewind:#{user_id}:#{year}:#{name}:v#{REWIND_REPORT_VERSION}"end- def load_reports_from_cache(username, year)- reports = Discourse.redis.get(cache_key(username, year))+ def load_reports_from_cache(user_id, year)+ reports = Discourse.redis.get(cache_key(user_id, year))return nil if !reportsMultiJson.load(reports, symbolize_keys: true)end- def load_single_report_from_cache(username, year, name)- report = Discourse.redis.get(single_report_cache_key(username, year, name))+ def load_single_report_from_cache(user_id, year, name)+ report = Discourse.redis.get(single_report_cache_key(user_id, year, name))return nil if !reportMultiJson.load(report, symbolize_keys: true)end# NOTE: This only caches the first INITIAL_REPORT_COUNT reports.- def cache_reports(username, year, reports)- Discourse.redis.setex(cache_key(username, year), CACHE_DURATION, MultiJson.dump(reports))+ def cache_reports(user_id, year, reports)+ Discourse.redis.setex(cache_key(user_id, year), CACHE_DURATION, MultiJson.dump(reports))end- def cache_single_report(username, year, name, report)+ def cache_single_report(user_id, year, name, report)Discourse.redis.setex(- single_report_cache_key(username, year, name),+ single_report_cache_key(user_id, year, name),CACHE_DURATION,MultiJson.dump(report),)diff --git a/plugins/discourse-rewind/spec/services/fetch_report_spec.rb b/plugins/discourse-rewind/spec/services/fetch_report_spec.rbindex f5a30e9e226..7415000e42a 100644--- a/plugins/discourse-rewind/spec/services/fetch_report_spec.rb+++ b/plugins/discourse-rewind/spec/services/fetch_report_spec.rb@@ -72,7 +72,7 @@ RSpec.describe(DiscourseRewind::FetchReport) dobefore docached_report = { data: "cached top words", identifier: "top-words" }DiscourseRewind::FetchReportsHelper.cache_single_report(- current_user.username,+ current_user.id,2021,"TopWords",cached_report,@@ -94,7 +94,7 @@ RSpec.describe(DiscourseRewind::FetchReport) dobefore docached_report = { data: "cached reading time", identifier: "reading-time" }DiscourseRewind::FetchReportsHelper.cache_single_report(- current_user.username,+ current_user.id,2021,"ReadingTime",cached_report,@@ -115,7 +115,7 @@ RSpec.describe(DiscourseRewind::FetchReport) dobefore docached_report = { data: "cached reactions", identifier: "reactions" }DiscourseRewind::FetchReportsHelper.cache_single_report(- current_user.username,+ current_user.id,2021,"Reactions",cached_report,@@ -148,7 +148,7 @@ RSpec.describe(DiscourseRewind::FetchReport) docached =DiscourseRewind::FetchReportsHelper.load_single_report_from_cache(- current_user.username,+ current_user.id,2021,"TopWords",)@@ -169,7 +169,7 @@ RSpec.describe(DiscourseRewind::FetchReport) docached =DiscourseRewind::FetchReportsHelper.load_single_report_from_cache(- current_user.username,+ current_user.id,2021,"Reactions",)@@ -217,7 +217,7 @@ RSpec.describe(DiscourseRewind::FetchReport) dobefore docached_report = { data: "other user report", identifier: "top-words" }DiscourseRewind::FetchReportsHelper.cache_single_report(- other_user.username,+ other_user.id,2021,"TopWords",cached_report,@@ -248,7 +248,7 @@ RSpec.describe(DiscourseRewind::FetchReport) dobefore docached_report = { data: "other user report", identifier: "top-words" }DiscourseRewind::FetchReportsHelper.cache_single_report(- other_user.username,+ other_user.id,2021,"TopWords",cached_report,