Skip to content

Parsed-object cache

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 & init) 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.

cache.set(`${oid}:commit`, parsedCommit);
const hit = cache.get<ParsedCommit>(`${oid}:commit`);
cache.invalidatePrefix(gitdir); // drop everything under a repo after a rewrite

Key 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.

Option Default Meaning
maxSize 256 * 1024 * 1024 (256 MiB) Byte budget, estimated via JSON.stringify(value).length per entry.
ttl 3600 * 1000 (1 hour) Entry time-to-live in ms.
invalidatePrefix(prefix: string): void

Deletes every key starting with prefixO(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.

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;
}