From dca0c6c933783806b4a9148ea60ccf59c1f8a1f7 Mon Sep 17 00:00:00 2001 From: Jan Novak Date: Wed, 11 Mar 2026 11:59:53 +0100 Subject: [PATCH] feat: warm up cache on app startup for fast first page load Pre-fetches all 4 cached datasets (attendance, juniors, payments, exceptions) at module load time so the first request doesn't block on Google API calls. Co-Authored-By: Claude Opus 4.6 --- app.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app.py b/app.py index 9306f97..49686d0 100644 --- a/app.py +++ b/app.py @@ -55,7 +55,24 @@ def get_month_labels(sorted_months, merged_months): labels[m] = dt.strftime("%b %Y") return labels +def warmup_cache(): + """Pre-fetch all cached data so first request is fast.""" + logger = logging.getLogger(__name__) + logger.info("Warming up cache...") + credentials_path = CREDENTIALS_PATH + + get_cached_data("attendance_regular", ATTENDANCE_SHEET_ID, get_members_with_fees) + get_cached_data("attendance_juniors", ATTENDANCE_SHEET_ID, get_junior_members_with_fees) + get_cached_data("payments_transactions", PAYMENTS_SHEET_ID, fetch_sheet_data, PAYMENTS_SHEET_ID, credentials_path) + get_cached_data("exceptions_dict", PAYMENTS_SHEET_ID, fetch_exceptions, + PAYMENTS_SHEET_ID, credentials_path, + serialize=lambda d: [[list(k), v] for k, v in d.items()], + deserialize=lambda c: {tuple(k): v for k, v in c}, + ) + logger.info("Cache warmup complete.") + app = Flask(__name__) +warmup_cache() @app.before_request def start_timer():