fix: expire schedule and attendee caches after one hour

This commit is contained in:
lucio committed 2026-09-30 01:39:33 -07:00
1 parent dd5a6ef5ae
commit cd7924d890
5 files changed
+61 -13

No files matched your search

+13 -5
View File
@@ -1,9 +1,8 @@
"""Local SQLite cache.
Wodify data for a past day does not change, and even today's schedule changes
slowly, so a same-day cache removes almost all network reads. Anything fetched
is stored with the local date it was fetched on; a later query on that same date
is served from disk.
Schedules and attendee rosters expire after one hour because reservations and
sign-in status change during the day. Workout content keeps a same-day cache.
All payloads also expire at the local calendar-day boundary.
stdlib only (``sqlite3``), keeping the query path dependency-free.
"""
@@ -17,6 +16,8 @@ import sqlite3
import stat
from typing import Any, Iterable
DYNAMIC_PAYLOAD_TTL = _dt.timedelta(hours=1)
SCHEMA = """
CREATE TABLE IF NOT EXISTS payloads (
key TEXT PRIMARY KEY,
@@ -78,13 +79,20 @@ def connect(path: pathlib.Path | None = None) -> sqlite3.Connection:
def get_payload(conn: sqlite3.Connection, key: str,
*, on: str | None = None) -> tuple[Any, str] | None:
"""Return ``(data, fetched_at)`` if cached on ``on`` (default today)."""
"""Return a same-day payload, with a one-hour TTL for dynamic data."""
row = conn.execute(
"SELECT body, fetched_at FROM payloads WHERE key = ? AND fetched_on = ?",
(key, on or today()),
).fetchone()
if row is None:
return None
if key.startswith(("schedule:", "attendees:")):
try:
age = _dt.datetime.now() - _dt.datetime.fromisoformat(row["fetched_at"])
except (ValueError, TypeError):
return None
if not _dt.timedelta(0) <= age < DYNAMIC_PAYLOAD_TTL:
return None
try:
return json.loads(row["body"]), row["fetched_at"]
except json.JSONDecodeError: