feat: add persistent json cache with background updates
Co-authored-by: aider (openrouter/thinkingmachines/inkling-small:free) <aider@aider.chat>
This commit is contained in:
1 parent
5d10b660d6
commit
ebc8196c02
1 file changed
+32
-2
+32
-2
@@ -4,6 +4,7 @@ import subprocess
|
|||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
import threading
|
import threading
|
||||||
|
import os
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
@@ -15,10 +16,38 @@ app.add_middleware(
|
|||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
)
|
)
|
||||||
|
|
||||||
cache = {}
|
CACHE_FILE = "cache.json"
|
||||||
CACHE_TTL = 300 # 5 minutes
|
CACHE_TTL = 300 # 5 minutes
|
||||||
|
cache = {}
|
||||||
cache_lock = threading.Lock()
|
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):
|
def run_kubectl(args, context=None):
|
||||||
cmd = ["kubectl"]
|
cmd = ["kubectl"]
|
||||||
if context:
|
if context:
|
||||||
@@ -43,6 +72,7 @@ def set_cached_items(resource, namespace, context, items):
|
|||||||
key = get_cache_key(resource, namespace, context)
|
key = get_cache_key(resource, namespace, context)
|
||||||
with cache_lock:
|
with cache_lock:
|
||||||
cache[key] = (time.time(), items)
|
cache[key] = (time.time(), items)
|
||||||
|
save_persistent_cache()
|
||||||
|
|
||||||
def refresh_worker(resource, namespace, context, kubectl_args, item_mapper):
|
def refresh_worker(resource, namespace, context, kubectl_args, item_mapper):
|
||||||
while True:
|
while True:
|
||||||
@@ -61,7 +91,7 @@ def refresh_worker(resource, namespace, context, kubectl_args, item_mapper):
|
|||||||
|
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
def start_background_refresh():
|
def start_background_refresh():
|
||||||
# Background renewal for common default namespace / no context
|
load_persistent_cache()
|
||||||
workers = [
|
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")}),
|
("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": "-"}),
|
("nodes", "default", None, ["get", "nodes", "-o", "json"], lambda i: {"name": i.get("metadata", {}).get("name", "unknown"), "namespace": "-"}),
|
||||||
|
|||||||
Reference in new issue
Block a user