41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
import json, hashlib
|
|
from pathlib import Path
|
|
|
|
|
|
class DiskCache:
|
|
def __init__(self, cache_dir: str = ".cache"):
|
|
self.cache_dir = Path(cache_dir)
|
|
self.cache_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
def _key_path(self, key: str) -> Path:
|
|
h = hashlib.sha256(key.encode()).hexdigest()
|
|
return self.cache_dir / f"{h}.json"
|
|
|
|
def get(self, key: str):
|
|
path = self._key_path(key)
|
|
if path.exists():
|
|
return json.loads(path.read_text())
|
|
return None
|
|
|
|
def set(self, key: str, value):
|
|
self._key_path(key).write_text(json.dumps(value))
|
|
|
|
def invalidate(self, key: str):
|
|
p = self._key_path(key)
|
|
if p.exists():
|
|
p.unlink()
|
|
|
|
|
|
class ReportStore:
|
|
def __init__(self, base_dir: str = "./reports"):
|
|
self.base_dir = Path(base_dir)
|
|
|
|
def save_history(self, program: str, status: str, matched: int, duration: float):
|
|
trend = self.base_dir / "trends" / f"{program}.jsonl"
|
|
trend.parent.mkdir(parents=True, exist_ok=True)
|
|
import datetime
|
|
entry = {"ts": datetime.datetime.now().isoformat(), "status": status,
|
|
"fields_matched": matched, "duration_s": duration}
|
|
with open(trend, "a") as f:
|
|
f.write(json.dumps(entry) + "\n")
|