feat: Implement caching for fetched items to improve startup performance and ignore cache files.

This commit is contained in:
2026-03-14 16:43:59 -07:00
parent b69515238c
commit 8f95ee5df9
2 changed files with 37 additions and 1 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
__pycache__/
*.pyc
.mainline_venv/
.mainline_cache_*.json

View File

@@ -460,6 +460,35 @@ def fetch_poetry():
return items, linked, failed
# ─── CACHE ────────────────────────────────────────────────
_CACHE_DIR = pathlib.Path(__file__).resolve().parent
def _cache_path():
return _CACHE_DIR / f".mainline_cache_{MODE}.json"
def _load_cache():
"""Load cached items from disk if available."""
p = _cache_path()
if not p.exists():
return None
try:
data = json.loads(p.read_text())
items = [tuple(i) for i in data["items"]]
return items if items else None
except Exception:
return None
def _save_cache(items):
"""Save fetched items to disk for fast subsequent runs."""
try:
_cache_path().write_text(json.dumps({"items": items}))
except Exception:
pass
# ─── STREAM ───────────────────────────────────────────────
_SCROLL_DUR = 3.75 # seconds per headline
FIREHOSE_H = 12 # firehose zone height (terminal rows)
@@ -868,7 +897,11 @@ def main():
print()
time.sleep(0.4)
if MODE == 'poetry':
cached = _load_cache() if '--refresh' not in sys.argv else None
if cached:
items = cached
boot_ln("Cache", f"LOADED [{len(items)} SIGNALS]", True)
elif MODE == 'poetry':
slow_print(" > INITIALIZING LITERARY CORPUS...\n")
time.sleep(0.2)
print()
@@ -876,6 +909,7 @@ def main():
print()
print(f" {G_DIM}>{RST} {G_MID}{linked} TEXTS LOADED{RST} {W_GHOST}· {failed} DARK{RST}")
print(f" {G_DIM}>{RST} {G_MID}{len(items)} STANZAS ACQUIRED{RST}")
_save_cache(items)
else:
slow_print(" > INITIALIZING FEED ARRAY...\n")
time.sleep(0.2)
@@ -884,6 +918,7 @@ def main():
print()
print(f" {G_DIM}>{RST} {G_MID}{linked} SOURCES LINKED{RST} {W_GHOST}· {failed} DARK{RST}")
print(f" {G_DIM}>{RST} {G_MID}{len(items)} SIGNALS ACQUIRED{RST}")
_save_cache(items)
if not items:
print(f"\n {W_DIM}> NO SIGNAL — check network{RST}")