Attempt to speed up leaderboard index with cache

This commit is contained in:
Max Wofford
2025-05-05 16:46:20 -04:00
parent 812db8da89
commit 9072ff9980

View File

@@ -15,12 +15,16 @@ class LeaderboardsController < ApplicationController
Date.current
end
@leaderboard = Leaderboard.where.not(finished_generating_at: nil)
.find_by(
start_date: start_date,
period_type: @period_type,
deleted_at: nil
)
cache_key = "leaderboard_#{@period_type}_#{start_date}"
@leaderboard = Rails.cache.fetch(cache_key, expires_in: 1.minute) do
Leaderboard.where.not(finished_generating_at: nil)
.find_by(
start_date: start_date,
period_type: @period_type,
deleted_at: nil
)
end
Rails.cache.delete(cache_key) if @leaderboard.nil?
if @leaderboard.nil?
LeaderboardUpdateJob.perform_later @period_type