memory / scratchpad.py — Scratchpad
In-memory LRU + optional SQLite persistence with per-entry TTL (default 24 h).
Storage
- OrderedDict in process memory (LRU).
- Optional SQLite at
persist_path/scratchpad.dbwith tablescratchpad(key PK, value JSON, created_at REAL, accessed_at REAL).
API
| Method | Purpose |
|---|---|
__init__(max_entries=50, persist_path=None, ttl=86400) | LRU + TTL config. |
set(key, value) -> str | Store; LRU evict if over capacity. |
get(key) -> Optional[Any] | Retrieve; TTL check; refresh accessed_at (sliding-window TTL). |
list_all() / count() / delete(key) / clear() | Standard helpers. |
Eviction model
- Age-based: any read computes
cutoff = now − ttl; expired entries are deleted lazily. - Capacity-based: oldest LRU entry evicted when capacity exceeded.
- Initial load on startup re-reads only non-expired entries.
threading.RLock guards in-memory dict + SQLite.