UNPKG

@chrono-cache/core

Version:

Backbone of chrono-cache, designed to provide a flexible and efficient approach to cache management

101 lines (96 loc) 2.27 kB
import fs from 'node:fs'; type TLRUCalculateSize<T> = (value: T) => string; type TCachedValue$1<TValue> = { value: TValue; tags: string[]; lastModified: number; expiresAt: number; }; interface ILRUCacheProps<TValue> { /** * Max size of value in Bytes */ maxSize: number; /** * Enable logs */ debug?: boolean; /** * TTL in milliseconds */ ttl: number; /** * Function to serialize value */ serializeValue: TLRUCalculateSize<TValue>; } declare class LRUCache<TValue> { private memoryCache; private cacheSizes; private serializeValue; private maxSize; private totalSize; private ttl; private debug; constructor(options: ILRUCacheProps<TValue>); /** * Get memory cache value */ get(key: string): TCachedValue$1<TValue> | null; /** * Set memory value */ set(key: string, value: TValue, tags?: string[]): void; has(key: string): boolean; private touch; private evictIfNecessary; private evictLeastRecentlyUsed; private getOldestCacheKey; private calculateSize; /** * Revalidate using tags array */ revalidateTags(tags: string[]): void; } interface NodeFs { existsSync: typeof fs.existsSync; readFile: typeof fs.promises.readFile; readFileSync: typeof fs.readFileSync; writeFile(f: string, d: string | Buffer): Promise<void>; mkdir(dir: string): Promise<string | void>; stat(f: string): Promise<{ mtime: Date; }>; } type TCachedValue = { value: string | Buffer; tags: string[]; lastModified: number; }; interface IFileCacheProps { fs: NodeFs; dir: string; debug?: boolean; } declare class FileCache { private distDir; private debug; private fs; private manifestManager; constructor(options: IFileCacheProps); /** * Get cached value */ get(key: string): Promise<TCachedValue | null>; /** * set cache */ set(key: string, data: TCachedValue["value"], tags?: string[]): Promise<void>; /** * Revalidate cache using tags */ revalidateTags(tags: string[]): Promise<void>; private isExpired; private getFilePath; } export { FileCache, LRUCache };