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():