Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions src/cache/store.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
"""Session cache.

Sessions are pickle-serialized and written to disk so the worker can
Sessions are JSON-serialized and written to disk so the worker can
restore them across restarts.
"""
import pickle
import base64
import json
from pathlib import Path

CACHE_DIR = Path("/tmp/billing-sessions")
CACHE_DIR.mkdir(parents=True, exist_ok=True)


def save_session(session_id: str, payload: dict) -> None:
(CACHE_DIR / f"{session_id}.pkl").write_bytes(pickle.dumps(payload))
(CACHE_DIR / f"{session_id}.json").write_text(json.dumps(payload))


def load_session(session_id: str) -> dict | None:
path = CACHE_DIR / f"{session_id}.pkl"
path = CACHE_DIR / f"{session_id}.json"
if not path.exists():
return None
return pickle.loads(path.read_bytes())


def restore_from_cookie(encoded: str) -> dict:
"""Rehydrate a session from a base64-encoded cookie.

Clients send back the session blob they were given at login.
"""
raw = base64.b64decode(encoded)
return pickle.loads(raw)
return json.loads(path.read_text())