Parsed-object cache
createParsedObjectCache — a generic LRU for any parsed value, keyed however you like.
import { createParsedObjectCache } from 'git-edge';
const cache = createParsedObjectCache({ maxSize: 128 * 1024 * 1024, ttl: 3600_000 });isomorphic-git's own packfile cache object (see
Repo cache) only saves you
from re-parsing a pack index. Higher-level parsed results built on top of
that — a rendered commit list, a resolved merge base, a formatted diff — get
recomputed on every call unless something caches them too. createParsedObjectCache
is a generic in-memory LRU for exactly those values.
Usage
cache.set(`${oid}:commit`, parsedCommit);
const hit = cache.get<ParsedCommit>(`${oid}:commit`);
cache.invalidatePrefix(gitdir); // drop everything under a repo after a rewriteKey convention is caller-chosen — `${oid}:${format}` (e.g. "<sha>:commit")
works well because git objects are content-addressed and immutable, so a key
built from an oid never goes stale on its own. invalidatePrefix exists for
the cases where staleness comes from outside the object graph — a rename, a
resync, a repack that rewrites storage layout underneath a long-lived process.
Options
| Option | Default | Meaning |
|---|---|---|
maxSize | 256 * 1024 * 1024 (256 MiB) | Approximate byte budget; each entry is sized by walking its value (no serialization). |
ttl | 3600 * 1000 (1 hour) | Entry time-to-live in ms. |
Size is estimated, not measured — set() walks the value to size it
rather than serializing it, so it never allocates a JSON copy of a large
cached value. It's a reasonable proxy for "how much memory does this
roughly cost," not a precise byte count.
invalidatePrefix
invalidatePrefix(prefix: string): voidDeletes every key starting with prefix — O(cache size), since it iterates
every key checking startsWith. Fine for occasional invalidation (after a
rewrite event), not something to call per-request or in a hot path.
Interface
The returned ParsedObjectStore is intentionally small and JS-value-generic
(not tied to any particular parsed shape):
interface ParsedObjectStore {
get<T extends object>(key: string): T | null;
set<T extends object>(key: string, value: T): void;
delete(key: string): void;
invalidatePrefix(prefix: string): void;
}