#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Shared cache invalidation marker for the PChome growth opportunity API.""" from __future__ import annotations import time from pathlib import Path _BASE_DIR = Path(__file__).resolve().parents[1] _CACHE_MARKER_FILE = _BASE_DIR / "data" / "pchome_growth_cache_epoch.txt" _MEMORY_EPOCH = 0.0 def get_pchome_growth_cache_epoch() -> float: """Return the newest known invalidation epoch across workers.""" global _MEMORY_EPOCH try: raw = _CACHE_MARKER_FILE.read_text(encoding="utf-8").strip() file_epoch = float(raw or 0) except (OSError, ValueError): file_epoch = 0.0 _MEMORY_EPOCH = max(_MEMORY_EPOCH, file_epoch) return _MEMORY_EPOCH def mark_pchome_growth_cache_stale() -> float: """Invalidate cached PChome growth opportunities in every worker.""" global _MEMORY_EPOCH epoch = time.time() _MEMORY_EPOCH = max(_MEMORY_EPOCH, epoch) try: _CACHE_MARKER_FILE.parent.mkdir(parents=True, exist_ok=True) _CACHE_MARKER_FILE.write_text(f"{epoch:.6f}", encoding="utf-8") except OSError: pass return _MEMORY_EPOCH