feat(map): add cached public station navigation

This commit is contained in:
lingniu
2026-08-12 10:50:06 +08:00
parent 357dd490e9
commit 6ef98d03c2
13 changed files with 780 additions and 4 deletions
+15 -2
View File
@@ -31,8 +31,8 @@ PORT = int(os.getenv("VEHICLE_MAP_PORT", "20800"))
OPEN_PLATFORM_BASE_URL = os.getenv("OPEN_PLATFORM_BASE_URL", "https://open.d.lnoneos.com").rstrip("/")
OPEN_PLATFORM_APP_KEY = os.getenv("OPEN_PLATFORM_APP_KEY", "").strip()
UPSTREAM_TIMEOUT_SECONDS = float(os.getenv("UPSTREAM_TIMEOUT_SECONDS", "15"))
DASHBOARD_CACHE_SECONDS = int(os.getenv("DASHBOARD_CACHE_SECONDS", "5"))
STATION_CACHE_SECONDS = int(os.getenv("STATION_CACHE_SECONDS", "3600"))
DASHBOARD_CACHE_SECONDS = int(os.getenv("DASHBOARD_CACHE_SECONDS", "120"))
STATION_CACHE_SECONDS = int(os.getenv("STATION_CACHE_SECONDS", "120"))
_cache_lock = threading.Lock()
_cache = {}
@@ -149,6 +149,18 @@ def _load_dashboard():
}
def _prewarm_dashboard_cache():
"""Warm the first dashboard snapshot after a process restart.
A warm snapshot lets the first visitor receive the cached response instead
of waiting for the three upstream requests to complete.
"""
try:
_cached("dashboard", DASHBOARD_CACHE_SECONDS, _load_dashboard)
except Exception as exc: # A later request can retry; startup must stay available.
print(f"vehicle map cache warmup deferred: {exc}")
class VehicleMapHandler(SimpleHTTPRequestHandler):
server_version = "LingniuVehicleMap/1.0"
@@ -215,6 +227,7 @@ class VehicleMapHandler(SimpleHTTPRequestHandler):
if __name__ == "__main__":
server = ThreadingHTTPServer((HOST, PORT), VehicleMapHandler)
threading.Thread(target=_prewarm_dashboard_cache, daemon=True).start()
print(f"Lingniu Vehicle Map listening on http://{HOST}:{PORT}")
try:
server.serve_forever()