Skip to content

CacheDecorator

katanakit-js / CacheDecorator

function CacheDecorator<T>(
fn,
options?,
): T & {
useCacheSize: () => number;
useClearCache: () => void;
};

Defined in: infrastructure/decorators/cache.decorator.ts:70

Decorates an async function with memoized results (Decorator Pattern).

Type Parameter Description
T extends (…args) => Promise<unknown> The async function type.
Parameter Type Description
fn T The function to decorate.
options? CacheDecoratorOptions TTL and key strategy.

T & { useCacheSize: () => number; useClearCache: () => void; }

The decorated function with an attached cache registry.

const getCached = CacheDecorator(async (id: number) => fetchUser(id), {
ttlMs: 30_000,
});