simple-on-disk-cache
Version:
A simple on-disk cache, supporting local and remote filesystem targets, with time based expiration policies.
54 lines (53 loc) • 1.51 kB
TypeScript
import { UniDuration } from '@ehmpathy/uni-time';
export interface SimpleOnDiskCache {
/**
* get a value from cache by key
*/
get: (key: string) => Promise<string | undefined>;
/**
* set a value to cache for key
*/
set: (key: string, value: string | undefined | Promise<string | undefined>, options?: {
expiration?: UniDuration | null;
}) => Promise<void>;
/**
* list all valid keys in cache
*/
keys: () => Promise<string[]>;
}
export declare const RESERVED_CACHE_KEY_FOR_VALID_KEYS = "_.simple_on_disk_cache.valid_keys";
/**
* the directory to persist your cache to can be either locally mounted or remote
*
* supported remote options:
* - AWS S3
*/
export type DirectoryToPersistTo = {
mounted: {
path: string;
};
} | {
s3: {
bucket: string;
prefix: string;
};
};
/**
* a utility function for deciding whether a record is valid
*/
export declare const isRecordExpired: ({ expiresAtMse, }: {
expiresAtMse: number | number;
}) => boolean;
/**
* create a simple on-disk cache
*/
export declare const createCache: ({ directory: directoryToPersistToInput, expiration: defaultExpiration, }: {
/**
* .what = the directory into which to persist the cache
*/
directory: DirectoryToPersistTo | (() => Promise<DirectoryToPersistTo>);
/**
* .what = how long to keep items cached until they expire, by default
*/
expiration?: UniDuration | null;
}) => SimpleOnDiskCache;