FIX: Only cache reports with exceptions for 1 minute (#11447)

This commit is contained in:
Mark VanLandingham 2020-12-09 10:54:41 -06:00 committed by GitHub
parent 06077856ce
commit 521934f163
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 4 deletions

View File

@ -42,7 +42,7 @@ class Admin::ReportsController < Admin::AdminController
report = Report.find(report_type, args)
if (report_params[:cache]) && report
Report.cache(report, 35.minutes)
Report.cache(report)
end
if report.blank?
@ -80,7 +80,7 @@ class Admin::ReportsController < Admin::AdminController
raise Discourse::NotFound if report.blank?
if (params[:cache])
Report.cache(report, 35.minutes)
Report.cache(report)
end
render_json_dump(report: report)

View File

@ -223,7 +223,8 @@ class Report
Discourse.cache.read(cache_key(report))
end
def self.cache(report, duration)
def self.cache(report)
duration = report.error == :exception ? 1.minute : 35.minutes
Discourse.cache.write(cache_key(report), report.as_json, expires_in: duration)
end

View File

@ -88,7 +88,7 @@ shared_examples "backup store" do
report_type = "storage_stats"
report = Report.find(report_type)
Report.cache(report, 35.minutes)
Report.cache(report)
expect(Report.find_cached(report_type)).to be_present
store.reset_cache

View File

@ -1237,6 +1237,33 @@ describe Report do
end
end
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" do
context "with no data" do
it "works" do