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.
This commit is contained in:
2026-05-17 11:25:50 +02:00
parent c93c473e38
commit beba7dc9ec

14
auth.py
View File

@@ -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)