@kolodny/lru-ttl-cache
Version:
Fast LRU and TTL cache with upsert and promise option
79 lines (78 loc) • 2.47 kB
TypeScript
export interface ConstOptions<K, V> {
max?: number;
maxBytes?: number | string;
ttl?: number | string;
ttlInterval?: number | string;
upsert?: ((key: K, additionalArgs?: any[]) => UpserResult<V> | Promise<UpserResult<V>>) | undefined;
}
export interface UpserResult<V> {
value: V;
bytes?: number;
isPermanent?: boolean;
}
interface NodeChain {
_prev?: NodeChain;
_next?: NodeChain;
}
interface Node<K, V> extends NodeChain {
value: V | Promise<V>;
key: K;
bytes: number;
createdAt: number;
lastAccess: number;
isPermanent: Boolean;
}
declare type NodeReadOnly<K, V> = Readonly<Node<K, V>>;
export default class LRU_TTL<K, V> implements NodeChain {
private _map;
private _max;
private _maxBytes;
private _ttl;
private _ttlInterval;
private _ttlP?;
private _upsert?;
private _tmpSize;
private _totalBytes;
private _tmpBytes;
_next: NodeChain;
_prev: NodeChain;
constructor(options?: ConstOptions<K, V>);
get max(): number;
set max(max: number);
get maxBytes(): number | string;
set maxBytes(maxBytes: number | string);
get ttl(): number | string;
set ttl(ttl: number | string);
get ttlInterval(): number | string;
set ttlInterval(ttlInterval: number | string);
get upsertCb(): ConstOptions<K, V>["upsert"];
set upsertCb(cb: ConstOptions<K, V>["upsert"]);
get bytes(): number;
get tmpBytes(): number;
get size(): number;
get tmpSize(): number;
has(key: K): boolean;
set(key: K, value: V | Promise<V>, bytes?: number, isPermanent?: boolean): this;
setPermanent(key: K, value: V, bytes?: number): this;
private _set;
get(key: K, upsert?: boolean, additionalUpsertCbArgs?: any[]): V | Promise<V> | undefined;
peek(key: K): V | Promise<V> | undefined;
pop(): V | Promise<V> | undefined;
getLRU(): V | Promise<V> | undefined;
upsert(key: K, ...args: any[]): V | Promise<V>;
delete(key: K): this;
private _delete;
clearTemp(): void;
clearAll(): void;
entries(): IterableIterator<[
K,
V | Promise<V>
]>;
keys(): IterableIterator<K>;
values(): IterableIterator<V | Promise<V>>;
forEach(cb: (value: V | Promise<V>, key: K) => void, thisArg: any): void;
_ttlClean(): void;
[Symbol.iterator](): Generator<(K | V | Promise<V>)[], void, unknown>;
getMetadata(key: K): NodeReadOnly<K, V> | undefined;
}
export {};