faastjs
Version:
Serverless batch computing made simple.
86 lines (85 loc) • 3.12 kB
TypeScript
/// <reference types="node" />
/**
* A simple persistent key-value store. Used to implement {@link Limits.cache}
* for {@link throttle}.
* @remarks
* Entries can be expired, but are not actually deleted individually. The entire
* cache can be deleted at once. Hence this cache is useful for storing results
* that are expensive to compute but do not change too often (e.g. the
* node_modules folder from an 'npm install' where 'package.json' is not
* expected to change too often).
*
* By default faast.js will use the directory `~/.faastjs` as a local cache to
* store data such as pricing retrieved from cloud APIs, and garbage collection
* information. This directory can be safely deleted if no faast.js instances
* are running.
* @public
*/
export declare class PersistentCache {
/**
* The directory under the user's home directory that will be used to
* store cached values. The directory will be created if it doesn't
* exist.
*/
readonly dirRelativeToHomeDir: string;
/**
* The age (in ms) after which a cached entry is invalid. Default:
* `24*3600*1000` (1 day).
*/
readonly expiration: number;
private initialized;
private initialize;
/**
* The directory on disk where cached values are stored.
*/
readonly dir: string;
/**
* Construct a new persistent cache, typically used with {@link Limits} as
* part of the arguments to {@link throttle}.
* @param dirRelativeToHomeDir - The directory under the user's home
* directory that will be used to store cached values. The directory will be
* created if it doesn't exist.
* @param expiration - The age (in ms) after which a cached entry is
* invalid. Default: `24*3600*1000` (1 day).
*/
constructor(
/**
* The directory under the user's home directory that will be used to
* store cached values. The directory will be created if it doesn't
* exist.
*/
dirRelativeToHomeDir: string,
/**
* The age (in ms) after which a cached entry is invalid. Default:
* `24*3600*1000` (1 day).
*/
expiration?: number);
private hash;
/**
* Retrieves the value previously set for the given key, or undefined if the
* key is not found.
*/
get(key: string): Promise<Buffer | undefined>;
/**
* Set the cache key to the given value.
* @returns a Promise that resolves when the cache entry has been persisted.
*/
set(key: string, value: Buffer | string | Uint8Array): Promise<void>;
/**
* Retrieve all keys stored in the cache, including expired entries.
*/
entries(): Promise<string[]>;
/**
* Deletes all cached entries from disk.
* @param leaveEmptyDir - If true, leave the cache directory in place after
* deleting its contents. If false, the cache directory will be removed.
* Default: `true`.
*/
clear({ leaveEmptyDir }?: {
leaveEmptyDir?: boolean | undefined;
}): Promise<void>;
}
export declare const caches: {
awsPrices: PersistentCache;
awsGc: PersistentCache;
};