A platform for community discussion. Free, open, simple.
FIX: Only cache reports with exceptions for 1 minute (#11447)
Mark VanLandingham521934f1638b
diff --git a/app/controllers/admin/reports_controller.rb b/app/controllers/admin/reports_controller.rbindex 5fc2544b482..e9be087108b 100644--- a/app/controllers/admin/reports_controller.rb+++ b/app/controllers/admin/reports_controller.rb@@ -42,7 +42,7 @@ class Admin::ReportsController < Admin::AdminControllerreport = Report.find(report_type, args)if (report_params[:cache]) && report- Report.cache(report, 35.minutes)+ Report.cache(report)endif report.blank?@@ -80,7 +80,7 @@ class Admin::ReportsController < Admin::AdminControllerraise Discourse::NotFound if report.blank?if (params[:cache])- Report.cache(report, 35.minutes)+ Report.cache(report)endrender_json_dump(report: report)diff --git a/app/models/report.rb b/app/models/report.rbindex 9a62774d9e1..b0f97d7ae5b 100644--- a/app/models/report.rb+++ b/app/models/report.rb@@ -223,7 +223,8 @@ class ReportDiscourse.cache.read(cache_key(report))end- def self.cache(report, duration)+ def self.cache(report)+ duration = report.error == :exception ? 1.minute : 35.minutesDiscourse.cache.write(cache_key(report), report.as_json, expires_in: duration)enddiff --git a/spec/lib/backup_restore/shared_examples_for_backup_store.rb b/spec/lib/backup_restore/shared_examples_for_backup_store.rbindex 679bc9aa79e..f6d3fbab3d2 100644--- a/spec/lib/backup_restore/shared_examples_for_backup_store.rb+++ b/spec/lib/backup_restore/shared_examples_for_backup_store.rb@@ -88,7 +88,7 @@ shared_examples "backup store" doreport_type = "storage_stats"report = Report.find(report_type)- Report.cache(report, 35.minutes)+ Report.cache(report)expect(Report.find_cached(report_type)).to be_presentstore.reset_cachediff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rbindex 576c713125d..0b4c330af82 100644--- a/spec/models/report_spec.rb+++ b/spec/models/report_spec.rb@@ -1237,6 +1237,33 @@ describe Report doendend+ describe ".cache" do+ let(:exception_report) { Report.find("exception_test", wrap_exceptions_in_test: true) }+ let(:valid_report) { Report.find("valid_test", wrap_exceptions_in_test: true) }++ before(:each) do+ class Report+ def self.report_exception_test(report)+ report.data = x+ end++ def self.report_valid_test(report)+ report.data = "success!"+ end+ end+ end++ it "caches exception reports for 1 minute" do+ Discourse.cache.expects(:write).with(Report.cache_key(exception_report), exception_report.as_json, { expires_in: 1.minute })+ Report.cache(exception_report)+ end++ it "caches valid reports for 35 minutes" do+ Discourse.cache.expects(:write).with(Report.cache_key(valid_report), valid_report.as_json, { expires_in: 35.minutes })+ Report.cache(valid_report)+ end+ end+describe "top_uploads" docontext "with no data" doit "works" do