igir
Version:
🕹 A zero-setup ROM collection manager that sorts, filters, extracts or archives, patches, and reports on collections of any size on any OS.
60 lines (59 loc) • 1.73 kB
TypeScript
export interface CacheProps {
filePath?: string;
fileFlushMillis?: number;
saveOnExit?: boolean;
}
/**
* A cache of a fixed size that ejects the oldest inserted key.
*/
export default class Cache<V> {
private static readonly BUFFER_ENCODING;
private keyValues;
private readonly keyedMutex;
private hasChanged;
private saveToFileTimeout?;
readonly filePath?: string;
readonly fileFlushMillis?: number;
private readonly saveMutex;
constructor(props?: CacheProps);
/**
* Return if a key exists in the cache, waiting for any existing operations to complete first.
*/
has(key: string): Promise<boolean>;
/**
* Return all the keys that exist in the cache.
*/
keys(): Set<string>;
/**
* Return the count of keys in the cache.
*/
size(): number;
/**
* Get the value of a key in the cache, waiting for any existing operations to complete first.
*/
get(key: string): Promise<V | undefined>;
/**
* Get the value of a key in the cache if it exists, or compute a value and set it in the cache
* otherwise.
*/
getOrCompute(key: string, runnable: (key: string) => V | Promise<V>, shouldRecompute?: (value: V) => boolean | Promise<boolean>): Promise<V>;
/**
* Set the value of a key in the cache.
*/
set(key: string, val: V): Promise<void>;
private setUnsafe;
/**
* Delete a key in the cache.
*/
delete(key: string | RegExp): Promise<void>;
private deleteUnsafe;
/**
* Load the cache from a file.
*/
load(): Promise<Cache<V>>;
private saveWithTimeout;
/**
* Save the cache to a file.
*/
save(): Promise<void>;
}