feat(map): add cached public station navigation
This commit is contained in:
+15
-2
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user