[verified] Add daily cache layer (fetch-once/day, stale fallback, atomic write)

- DailyCache: JSON file cache keyed by (source/as_of), TTL 24h, atomic tmp+rename write, persists across runs
- fetch_or_stale: returns fresh cache, else refetch+cache, else falls back to stale so dashboard is never blanked
- 7 tests; full suite pass
This commit is contained in:
Kunthawat Greethong committed 2026-08-25 15:29:15 +07:00
1 parent affc29a3be
commit 63058ecfc9
2 files changed
+170

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
"""Simple daily cache for alternative-factor data.
Per the user's requirement: fetch once per day, keep the raw value keyed by
(source, as_of), TTL ~24h, and fall back to the last good cached value when a
re-fetch fails (so an interrupted/nightly run doesn't blank the dashboard).
The cache is a plain JSON file under backend/data/alternative_cache/ — atomic
write (tmp + rename) so a crash mid-write can't corrupt it.
"""
from __future__ import annotations
import json
import os
import tempfile
import time
from pathlib import Path
from typing import Any, Optional
_DEFAULT_DIR = Path(__file__).resolve().parent.parent / "data" / "alternative_cache"
_DEFAULT_TTL = 24 * 3600 # 24h
class DailyCache:
def __init__(self, cache_dir: Path = _DEFAULT_DIR, ttl: float = _DEFAULT_TTL) -> None:
self.cache_dir = Path(cache_dir)
self.cache_dir.mkdir(parents=True, exist_ok=True)
self.ttl = ttl
self._index_path = self.cache_dir / "_index.json"
self._index: dict = self._load_index()
def _load_index(self) -> dict:
if self._index_path.exists():
try:
return json.loads(self._index_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
return {}
return {}
def _save_index(self) -> None:
tmp = self._index_path.with_suffix(".tmp")
tmp.write_text(json.dumps(self._index, ensure_ascii=False, sort_keys=True),
encoding="utf-8")
os.replace(tmp, self._index_path)
def _key_file(self, key: str) -> Path:
# key like "tourism/2026-08-25" -> nested path to avoid one huge dir
safe = key.replace("/", "__").replace(":", "_")
return self.cache_dir / f"{safe}.json"
def get(self, key: str) -> Optional[dict]:
entry = self._index.get(key)
if entry is None:
return None
path = self._key_file(key)
if not path.exists():
return None
try:
return json.loads(path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
return None
def fresh(self, key: str) -> bool:
entry = self._index.get(key)
if entry is None:
return False
return (time.time() - entry.get("ts", 0)) <= self.ttl
def set(self, key: str, value: dict) -> None:
path = self._key_file(key)
tmp = path.with_suffix(".tmp")
tmp.write_text(json.dumps(value, ensure_ascii=False, sort_keys=True),
encoding="utf-8")
os.replace(tmp, path)
self._index[key] = {"ts": time.time()}
self._save_index()
# Convenience: fetch-or-fallback
def fetch_or_stale(self, key: str, fetcher, *args, **kwargs) -> dict:
"""Return fresh cached value, else call fetcher and cache it, else stale."""
cached = self.get(key)
if cached is not None and self.fresh(key):
return cached
try:
value = fetcher(*args, **kwargs)
self.set(key, value)
return value
except Exception:
# fall back to stale cache so the dashboard is never blanked
if cached is not None:
cached = dict(cached)
cached["_stale"] = True
return cached
raise
+76
View File
@@ -0,0 +1,76 @@
"""Tests for the daily cache layer."""
from __future__ import annotations
import tempfile
import time
import unittest
from pathlib import Path
from app import daily_cache
class DailyCacheTest(unittest.TestCase):
def setUp(self) -> None:
self._tmp = tempfile.mkdtemp(prefix="cachemain_")
self.cache = daily_cache.DailyCache(cache_dir=Path(self._tmp), ttl=0.5)
def tearDown(self) -> None:
import shutil
shutil.rmtree(self._tmp, ignore_errors=True)
def test_set_get_roundtrip(self) -> None:
self.cache.set("tourism/2026-08-25", {"value": 123, "as_of": "2026-08"})
got = self.cache.get("tourism/2026-08-25")
self.assertIsNotNone(got)
self.assertEqual(got["value"], 123)
def test_missing_key_returns_none(self) -> None:
self.assertIsNone(self.cache.get("nope"))
def test_fresh_after_set(self) -> None:
self.cache.set("k", {"a": 1})
self.assertTrue(self.cache.fresh("k"))
def test_expired_not_fresh(self) -> None:
self.cache.set("k", {"a": 1})
time.sleep(0.7)
self.assertFalse(self.cache.fresh("k"))
def test_fetch_or_stale_caches_value(self) -> None:
calls = {"n": 0}
def fetcher():
calls["n"] += 1
return {"value": 42}
v1 = self.cache.fetch_or_stale("k", fetcher)
self.assertEqual(v1["value"], 42)
self.assertEqual(calls["n"], 1)
# second call within TTL -> cached, fetcher not re-invoked
v2 = self.cache.fetch_or_stale("k", fetcher)
self.assertIsNotNone(v2)
self.assertEqual(v2["value"], 42)
self.assertEqual(calls["n"], 1)
def test_fetch_or_stale_falls_back_on_error(self) -> None:
self.cache.set("k", {"value": 99})
def broken():
raise RuntimeError("network down")
# expired cache so it tries to refetch
time.sleep(0.7)
v = self.cache.fetch_or_stale("k", broken)
self.assertEqual(v["value"], 99)
self.assertTrue(v.get("_stale"))
def test_reload_persists(self) -> None:
self.cache.set("k", {"x": 1})
cache2 = daily_cache.DailyCache(cache_dir=Path(self._tmp), ttl=0.5)
self.assertEqual(cache2.get("k")["x"], 1)
if __name__ == "__main__":
unittest.main()