feat: warm up cache on app startup for fast first page load
All checks were successful
Build and Push / build (push) Successful in 8s

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 <noreply@anthropic.com>
This commit is contained in:
2026-03-11 11:59:53 +01:00
parent 9b99f6d33b
commit dca0c6c933

17
app.py
View File

@@ -55,7 +55,24 @@ def get_month_labels(sorted_months, merged_months):
labels[m] = dt.strftime("%b %Y") labels[m] = dt.strftime("%b %Y")
return labels 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__) app = Flask(__name__)
warmup_cache()
@app.before_request @app.before_request
def start_timer(): def start_timer():