molforge.cache¶
cache ¶
Result caching for molforge engines.
Engines (folding, docking, generative) take minutes-to-hours per
call. Recomputing identical inputs is wasteful, and molforge's
:class:Provenance makes it trivial to detect when a recomputation
is identical: hash the engine + parameters + inputs + parent chain,
look up the result.
Integration on the engine side is a few lines per method::
provenance = self._build_provenance(...)
cache = get_default_cache()
cached = cache.get(provenance, "protein")
if cached is not None:
return cached
result = self._actually_compute(...)
cache.put(provenance, result, "protein")
return result
What gets cached
- :class:
molforge.core.Proteinfrom folding wrappers. list[DesignedSequence]from generative wrappers.- :class:
molforge.docking.DockingResultfrom docking wrappers (Vina, Gnina, DiffDock). Extra result types register via :func:register_serializer.
What deliberately doesn't get cached:
- :class:molforge.md.Trajectory. Multi-GB per simulation; users
who want this should use the upstream MD framework's
checkpointing.
Cache location
- Default:
~/.cache/molforge/(XDG convention). - Overridable via
MOLFORGE_CACHE_DIR. - Disable globally with
MOLFORGE_CACHE=disabled.
Cache layout
One subdirectory per entry, named by SHA-256 of the canonical key. Each entry holds:
type: text file with the type tagmeta.json: Protein name + metadata (with arrays + Provenance replaced by markers)structure.cif(for Protein only): the AtomArray as mmCIFpayload.json(for DesignedSequence list): the design listreceptor.cif+pose_{i}.cif(for DockingResult): the receptor and each pose ligand as mmCIF, with scalar pose fields and metadata inpayload.jsonarrays.npz: numpy arrays from metadata when present
Safety
- Corrupted entries are treated as misses and logged; never crash.
- Molforge major.minor version is part of the key — version upgrades invalidate transparently.
- Timestamps are excluded from the key — different runs of the same computation share a slot.
- Writes go to a
.tmpdirectory and rename atomically.
Cache ¶
File-system-backed cache for engine results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
Path | str | None
|
Cache directory. |
None
|
enabled
|
bool | None
|
Master switch. |
None
|
get ¶
Look up a cached result. Returns None on miss or any error.
put ¶
Store a result. Errors are logged, never propagate.
contains ¶
True if a cache entry exists for this Provenance.
path_for ¶
On-disk path for an entry. May not exist (cache miss).
clear ¶
Delete every entry. Only removes hex-named directories (defensive — never touches anything else in the cache dir).
cache_key ¶
Return the canonical 64-char hex cache key for a Provenance.
Mixes the molforge major.minor version into the hash so version upgrades invalidate transparently. Timestamps are excluded so two runs of the same computation share a slot.
default_cache_dir ¶
Resolve the default cache directory.
Order: $MOLFORGE_CACHE_DIR → $XDG_CACHE_HOME/molforge →
~/.cache/molforge.
register_serializer ¶
Register a serializer/deserializer for a result type.
The type_tag is stored alongside the entry and dispatched on
lookup, so changing it invalidates previously-cached entries of
the same shape. Pick a stable string.
Re-registering overwrites; tests rely on that for cleanup.
get_default_cache ¶
Return a process-wide :class:Cache rooted at
:func:default_cache_dir.
Singleton — engines call this each time they want to consult the
cache. MOLFORGE_CACHE=disabled is honoured at first-call time.