CacheDecorator
katanakit-js / CacheDecorator
Function: CacheDecorator()
Section titled “Function: 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 Parameters
Section titled “Type Parameters”| Type Parameter | Description |
|---|---|
T extends (…args) => Promise<unknown> |
The async function type. |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
fn |
T |
The function to decorate. |
options? |
CacheDecoratorOptions |
TTL and key strategy. |
Returns
Section titled “Returns”T & {
useCacheSize: () => number;
useClearCache: () => void;
}
The decorated function with an attached cache registry.
Example
Section titled “Example”const getCached = CacheDecorator(async (id: number) => fetchUser(id), { ttlMs: 30_000,});