@modern-js/runtime-utils
Version:
A Progressive React Framework for modern web development.
69 lines (68 loc) • 2.22 kB
TypeScript
export declare const CacheSize: {
readonly KB: 1024;
readonly MB: number;
readonly GB: number;
};
export declare const CacheTime: {
readonly SECOND: 1000;
readonly MINUTE: number;
readonly HOUR: number;
readonly DAY: number;
readonly WEEK: number;
readonly MONTH: number;
};
export type CacheStatus = 'hit' | 'stale' | 'miss';
export interface CacheStatsInfo {
status: CacheStatus;
key: string;
params: any[];
result: any;
/**
* Cache miss reason:
* 1: Caching is disabled for the current request
* 2: Item not found in cache
* 3: Item found in cache but has expired
* 4: Failed to parse data from cache
* 5: Execution error
*/
reason?: number;
}
export interface Container {
get: (key: string) => Promise<any | undefined | null>;
set: (key: string, value: any, options?: {
ttl?: number;
}) => Promise<any>;
has: (key: string) => Promise<boolean>;
delete: (key: string) => Promise<boolean>;
clear: () => Promise<void>;
}
interface CacheOptions<T extends (...args: any[]) => any> {
tag?: string | string[];
maxAge?: number;
revalidate?: number;
getKey?: (...args: Parameters<T>) => string;
customKey?: (options: {
params: Parameters<T>;
fn: T;
generatedKey: string;
}) => string;
onCache?: (info: CacheStatsInfo) => void;
unstable_shouldCache?: (info: {
params: Parameters<T>;
result: Awaited<ReturnType<T>>;
}) => boolean | Promise<boolean>;
}
interface CacheConfig {
maxSize?: number;
container?: Container;
unstable_shouldDisable?: ({ request, }: {
request: Request;
}) => boolean | Promise<boolean>;
}
export declare function configureCache(config: CacheConfig): void;
export declare function generateKey(args: unknown[]): string;
export declare function cache<T extends (...args: any[]) => Promise<any>>(fn: T, options?: CacheOptions<T>): T;
export declare function withRequestCache<T extends (req: Request, ...args: any[]) => Promise<any>>(handler: T): T;
export declare function revalidateTag(tag: string): Promise<void>;
export declare function clearStore(): Promise<void>;
export {};