From beba7dc9ec6257501f9a994388a8c4de286c63c9 Mon Sep 17 00:00:00 2001 From: rafapolo Date: Sun, 17 May 2026 11:25:50 +0200 Subject: [PATCH] Pre-warm S3 object cache for hot tables on startup After init, a background thread runs COUNT(*) on the three most-queried tables so the first real user query hits a warm Parquet metadata cache. --- auth.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/auth.py b/auth.py index 97650fc..5d4b2e9 100644 --- a/auth.py +++ b/auth.py @@ -29,6 +29,20 @@ def _init_db(): SET http_retries=3; """) _con.execute("ATTACH '/app/data/basedosdados.duckdb' AS basedosdados (READ_ONLY)") + threading.Thread(target=_warm_cache, daemon=True).start() + +def _warm_cache(): + hot_tables = [ + "br_tse_eleicoes.candidatos", + "br_tse_eleicoes.despesas_candidato", + "br_tse_eleicoes.resultados_candidato", + ] + for t in hot_tables: + try: + with _lock: + _con.execute(f"SELECT COUNT(*) FROM basedosdados.{t}") + except Exception: + pass def _json_default(obj): if isinstance(obj, decimal.Decimal): return float(obj)