@m4x1m1l14n/cache
Version:
Lightweight in-memory isomorphic cache implementation with TTL for browser & Node JS written in TypeScript
50 lines (49 loc) • 1.55 kB
TypeScript
import { CacheOptions } from '../models/CacheOptions';
import { ExpirationCallback } from '../types';
export declare class Cache<K, T> {
private options;
private cache;
private timeoutHandle;
private expirationHeap;
private destroyed;
private ensureNotDestroyed;
constructor(options?: CacheOptions);
set(key: K, value: T, ttl?: number, callback?: ExpirationCallback<T>): Cache<K, T>;
/**
* Returns value by its key
*
* @param key Key of value to get
* @param refresh True to refresh item TTL or false to not
*/
get(key: K, refresh?: boolean): T | undefined;
/**
* Takes value by its key from cache.
*
* Value is returned and key is removed from cache.
*
* @param key Key to take
* @returns Value of specified key
*/
take(key: K): T | undefined;
has(key: K): boolean;
delete(key: K): boolean;
mdelete(keys: K[]): Cache<K, T>;
get size(): number;
forEach(cb: (value: T, key: K) => void): void;
private cleanup;
/**
* Finds the earliest expiration time among all cached items using the heap
* @returns The earliest expiration timestamp, or null if no items expire
*/
private findEarliestExpiration;
/**
* Schedules the next cleanup based on the earliest expiration time
*/
private scheduleCleanup;
/**
* Clears any scheduled cleanup timeout
*/
private clearScheduledCleanup;
destroy(invokeCallbacks?: boolean): void;
flush(invokeCallback?: boolean): void;
}