Initial commit
This commit is contained in:
commit
184b0e6033
18 changed files with 1178 additions and 0 deletions
44
full_updater/backend/cache.py
Normal file
44
full_updater/backend/cache.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import json
|
||||
import os
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
CACHE_DIR = "/tmp/full-updater-cache"
|
||||
|
||||
|
||||
def ensure_cache_dir() -> None:
|
||||
if os.path.exists(CACHE_DIR):
|
||||
shutil.rmtree(CACHE_DIR)
|
||||
os.makedirs(CACHE_DIR, exist_ok=True)
|
||||
|
||||
|
||||
def write_cache(target_id: str, data: dict[str, Any]) -> None:
|
||||
ensure_cache_dir()
|
||||
path = os.path.join(CACHE_DIR, f"{target_id}.json")
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
|
||||
def read_cache(target_id: str) -> dict[str, Any] | None:
|
||||
path = os.path.join(CACHE_DIR, f"{target_id}.json")
|
||||
if not os.path.exists(path):
|
||||
return None
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def get_cache_timestamp(target_id: str) -> str:
|
||||
path = os.path.join(CACHE_DIR, f"{target_id}.json")
|
||||
if not os.path.exists(path):
|
||||
return ""
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
ts = data.get("timestamp", "")
|
||||
if ts:
|
||||
dt = datetime.fromisoformat(ts)
|
||||
return dt.strftime("%H:%M:%S")
|
||||
except Exception:
|
||||
pass
|
||||
return ""
|
||||
Loading…
Add table
Add a link
Reference in a new issue