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.
This commit is contained in:
Joffrey JAFFEUX 2024-08-29 12:16:57 +02:00 committed by GitHub
parent 8b6c5da755
commit 4f705b3146
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 1 deletions

View File

@ -81,7 +81,7 @@ class About
end end
def stats def stats
@stats ||= About.fetch_stats @stats ||= About.fetch_cached_stats
end end
def category_moderators def category_moderators

View File

@ -15,6 +15,8 @@ RSpec.describe About do
after { DiscoursePluginRegistry.reset! } after { DiscoursePluginRegistry.reset! }
describe "#stats" do describe "#stats" do
use_redis_snapshotting
it "adds plugin stats to the output" do it "adds plugin stats to the output" do
stats = { :last_day => 1, "7_days" => 10, "30_days" => 100, :count => 1000 } stats = { :last_day => 1, "7_days" => 10, "30_days" => 100, :count => 1000 }
register_stat("some_group", Proc.new { stats }) register_stat("some_group", Proc.new { stats })
@ -28,6 +30,13 @@ RSpec.describe About do
) )
end 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 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 } stats = { "7_days" => 10, "30_days" => 100, :count => 1000 }
register_stat("some_group", Proc.new { stats }) register_stat("some_group", Proc.new { stats })