Skip to content
← Public packages

@kody/kody-factory

Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.

FollowFork

Files

mem-cache.ts

23 lines · 719 B · TypeScript
/**
 * Module-scoped in-memory cache with TTLs. No storage writes.
 * Entries are keyed by string; each has a TTL after which it is stale.
 */
const store = new Map<string, { data: unknown; at: number }>()

export function memGet<T>(key: string, ttlMs: number): T | undefined {
	const hit = store.get(key)
	if (!hit || Date.now() - hit.at > ttlMs) return undefined
	return hit.data as T
}

export function memSet<T>(key: string, data: T): void {
	store.set(key, { data, at: Date.now() })
}

export async function memCached<T>(key: string, ttlMs: number, fn: () => Promise<T>): Promise<T> {
	const hit = memGet<T>(key, ttlMs)
	if (hit !== undefined) return hit
	const data = await fn()
	memSet(key, data)
	return data
}