From 4f705b3146b737d8bf3e52436e8665f747e1529a Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 29 Aug 2024 12:16:57 +0200 Subject: [PATCH] FIX: ensure About#stats uses the cache (#28634) Prior to this fix we were calling `fetch_stats` which is never checking if we have a cache entry. This call is making a lot of SQL calls, so it's better to use the cache. --- app/models/about.rb | 2 +- spec/models/about_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/models/about.rb b/app/models/about.rb index 26c17495d64..d385e19c680 100644 --- a/app/models/about.rb +++ b/app/models/about.rb @@ -81,7 +81,7 @@ class About end def stats - @stats ||= About.fetch_stats + @stats ||= About.fetch_cached_stats end def category_moderators diff --git a/spec/models/about_spec.rb b/spec/models/about_spec.rb index 713ab2f22b9..bf70caeef71 100644 --- a/spec/models/about_spec.rb +++ b/spec/models/about_spec.rb @@ -15,6 +15,8 @@ RSpec.describe About do after { DiscoursePluginRegistry.reset! } describe "#stats" do + use_redis_snapshotting + it "adds plugin stats to the output" do stats = { :last_day => 1, "7_days" => 10, "30_days" => 100, :count => 1000 } register_stat("some_group", Proc.new { stats }) @@ -28,6 +30,13 @@ RSpec.describe About do ) end + it "uses the cache" do + cold_cache_count = track_sql_queries { described_class.new.stats }.count + hot_cache_count = track_sql_queries { described_class.new.stats }.count + + expect(cold_cache_count + hot_cache_count).to eq(cold_cache_count) + end + it "does not add plugin stats to the output if they are missing one of the required keys" do stats = { "7_days" => 10, "30_days" => 100, :count => 1000 } register_stat("some_group", Proc.new { stats })