Skip to content
0

katanakit-js / CacheDecorator

Function: CacheDecorator() ​

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

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

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

Type Parameters ​

Type ParameterDescription
T extends (...args) => Promise<unknown>The async function type.

Parameters ​

ParameterTypeDescription
fnTThe function to decorate.
optionsCacheDecoratorOptionsTTL and key strategy.

Returns ​

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

The decorated function with an attached cache registry.

Example ​

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

Released under the MIT License.