UNPKG

t7m

Version:

Transformer for Elysia and Hono

45 lines 1.52 kB
/** * A type-safe function cache. * Caches results by input - subsequent calls with the same input return the cached result. * For async functions, concurrent calls share the same promise. * * Requirements: * - The function must have exactly one argument * * @example * ```ts * const fetchUser = async (id: number) => db.users.findOne({ id }); * const cached = new Cache(fetchUser); * * await cached.call(1); // Executes function * await cached.call(1); // Returns cached result * ``` * * @example With selective cache keys * ```ts * const cached = new Cache(fetchUser, 'id'); // Only cache on 'id' key * ``` */ declare class Cache<FN extends (() => any) | ((arg: any) => any)> { readonly fn: FN; private readonly cacheOnObjectParams?; /** * Creates a new Cache instance. * @param fn - The function to cache * @param on - For object args: keys to use for cache key (default: all keys) */ constructor(fn: FN, ...on: Parameters<FN>[0] extends Record<string, unknown> ? (keyof Parameters<FN>[0])[] : []); private cache; private objectCacheKey; /** * Calls the cached function, returning cached promise if available. * @param arg - The argument to pass to the cached function * @returns The promise from cache or a new execution */ call(...args: Parameters<FN>): ReturnType<FN>; /** Clears all cached promises. */ clear: () => void; } export { Cache }; export type AnyCache = Cache<any>; //# sourceMappingURL=cache.d.ts.map