fn-merge-cache
Version:
`FnMergeCache` is a caching utility that allows functions to cache their results based on input arguments, with options for cache lifetime, size limits, error handling, and parameter comparison, while supporting cache invalidation via tags and global reva
80 lines (79 loc) • 3.06 kB
TypeScript
/**
* A class for merging and caching function calls
*
* @typeParam A - Tuple type of function parameters
* @typeParam R - Function return type
*/
export declare class FnMergeCache<A extends any[], R> {
private _disposed;
private _fn;
private _cache;
private _cacheOnError;
private _argComparer;
private _ttl;
private _maxCacheSize;
private _tags;
private _result;
private _callGC;
/**
* Creates a new FnMergeCache instance
*
* @param fn - The original function to be cached
* @param options - Configuration options
* @param options.cache - Whether to enable caching
* @param options.cacheOnError - Whether to cache results when errors occur
* @param options.argComparer - Parameter comparison function, returns true if parameters are equal
* @param options.ttl - Cache lifetime in milliseconds, 0 means never expires
* @param options.maxCacheSize - Cache pool size limit, 0 means no limit
* @param options.tags - Tags for cache revalidation
*
* @throws Error when using reserved tag names
*/
constructor(fn: (...args: A) => R, { cache, cacheOnError, argComparer, ttl, maxCacheSize, tags, }?: {
cache?: boolean | undefined;
cacheOnError?: boolean | undefined;
argComparer?: ((value: any, other: any) => boolean) | undefined;
ttl?: number | undefined;
maxCacheSize?: number | undefined;
tags?: string[] | undefined;
});
/**
* Calls the cached function
*
* @param args - Arguments passed to the original function
* @returns Function return value, may be cached result
* @throws Error if instance is disposed or original function throws
*/
call(...args: A): R;
/**
* Clears all cached results
*/
revalidate: () => void;
/**
* Destroys the instance, clears all caches and event listeners
*/
dispose(): void;
}
export default FnMergeCache;
/**
* Creates a function with caching capability
*
* @typeParam A - Tuple type of function parameters
* @typeParam R - Function return type
* @param fn - The original function to be cached
* @param opts - FnMergeCache configuration options
* @param opts.cache - Whether to enable caching
* @param opts.cacheOnError - Whether to cache results when errors occur
* @param opts.argComparer - Parameter comparison function, returns true if parameters are equal
* @param opts.ttl - Cache lifetime in milliseconds, 0 means never expires
* @param opts.maxCacheSize - Cache pool size limit, 0 means no limit
* @param opts.tags - Tags for cache revalidation
* @returns A new function with caching capability
*/
export declare function createMergedCachedFn<A extends any[], R>(fn: (...args: A) => R, opts: ConstructorParameters<typeof FnMergeCache>[1]): (...args: A) => R;
/**
* Invalidates cache for specified tags
*
* @param tag - Tag or array of tags to invalidate, defaults to invalidating all caches
*/
export declare function revalidateTag(tag?: string | string[]): void;