@joaquimserafim/ttl-lru-cache
Version:
A TypeScript cache implementation focused on time-based expiration with automatic cleanup and TTL support
54 lines (52 loc) • 1.5 kB
text/typescript
type DisposeReason = "delete" | "set" | "evict" | "stale";
interface TTLCacheOptions<K, V> {
max?: number;
ttl?: number;
updateAgeOnGet?: boolean;
checkAgeOnGet?: boolean;
noUpdateTTL?: boolean;
onRemove?: (value: V, key: K, reason: DisposeReason) => void;
skipRemoveOnSet?: boolean;
}
interface TTLCacheSetOptions {
ttl?: number;
noUpdateTTL?: boolean;
skipRemoveOnSet?: boolean;
}
declare class TTLCache<K, V> {
private expirations;
private data;
private expirationMap;
private ttl;
private max;
private updateAgeOnGet;
private checkAgeOnGet;
private noUpdateTTL;
private skipRemoveOnSet;
private timer?;
private timerExpiration?;
handleRemoval(value: V, key: K, reason: DisposeReason): void;
constructor(options?: TTLCacheOptions<K, V>);
private setTimer;
cancelTimer(): void;
clear(): void;
private setTTL;
set(key: K, val: V, options?: TTLCacheSetOptions): this;
has(key: K): boolean;
getRemainingTTL(key: K): number;
get(key: K, options?: {
updateAgeOnGet?: boolean;
ttl?: number;
checkAgeOnGet?: boolean;
}): V | undefined;
delete(key: K): boolean;
get size(): number;
private removeKeys;
private purgeToCapacity;
purgeStale(): void;
entries(): IterableIterator<[K, V]>;
keys(): IterableIterator<K>;
values(): IterableIterator<V>;
[Symbol.iterator](): IterableIterator<[K, V]>;
}
export { TTLCache };