From ebc8196c023009da6bfa23ad142426a028555986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Stach?= Date: Fri, 9 Oct 2026 18:44:51 +0200 Subject: [PATCH] feat: add persistent json cache with background updates Co-authored-by: aider (openrouter/thinkingmachines/inkling-small:free) --- backend/main.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/backend/main.py b/backend/main.py index 291be10..489374c 100644 --- a/backend/main.py +++ b/backend/main.py @@ -4,6 +4,7 @@ import subprocess import time import json import threading +import os app = FastAPI() @@ -15,10 +16,38 @@ app.add_middleware( allow_headers=["*"], ) -cache = {} +CACHE_FILE = "cache.json" CACHE_TTL = 300 # 5 minutes +cache = {} cache_lock = threading.Lock() +def load_persistent_cache(): + global cache + if not os.path.exists(CACHE_FILE): + return + try: + with open(CACHE_FILE, "r") as f: + data = json.load(f) + loaded = {} + for k, v in data.items(): + if isinstance(v, list) and len(v) == 2: + loaded[k] = (v[0], v[1]) + with cache_lock: + cache = loaded + except Exception: + pass + +def save_persistent_cache(): + try: + with cache_lock: + data = {} + for k, v in cache.items(): + data[k] = [v[0], v[1]] + with open(CACHE_FILE, "w") as f: + json.dump(data, f) + except Exception: + pass + def run_kubectl(args, context=None): cmd = ["kubectl"] if context: @@ -43,6 +72,7 @@ def set_cached_items(resource, namespace, context, items): key = get_cache_key(resource, namespace, context) with cache_lock: cache[key] = (time.time(), items) + save_persistent_cache() def refresh_worker(resource, namespace, context, kubectl_args, item_mapper): while True: @@ -61,7 +91,7 @@ def refresh_worker(resource, namespace, context, kubectl_args, item_mapper): @app.on_event("startup") def start_background_refresh(): - # Background renewal for common default namespace / no context + load_persistent_cache() workers = [ ("pods", "default", None, ["get", "pods", "-n", "default", "-o", "json"], lambda i: {"name": i.get("metadata", {}).get("name", "unknown"), "namespace": i.get("metadata", {}).get("namespace", "default")}), ("nodes", "default", None, ["get", "nodes", "-o", "json"], lambda i: {"name": i.get("metadata", {}).get("name", "unknown"), "namespace": "-"}),