mastercache
Version:
Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers
197 lines (192 loc) • 6.97 kB
TypeScript
import { CacheDriver } from './src/types/driver.js';
import { CacheProvider } from './src/types/provider.js';
import { RawCommonOptions, RawMasterCacheOptions } from './src/types/options/options.js';
import { f as CacheEvents } from './events-CkqPK7En.js';
import { GetPojoOptions, GetOptions, SetPojoOptions, SetOptions, GetOrSetPojoOptions, GetOrSetOptions, GetOrSetForeverPojoOptions, GetOrSetForeverOptions, HasPojoOptions, HasOptions, DeletePojoOptions, DeleteOptions, DeleteManyPojoOptions, ClearOptions } from './src/types/options/methods-options.js';
import { Factory, GetSetFactory } from './src/types/helpers.js';
import { BusOptions, BusDriver } from './src/types/bus.js';
import './src/types/options/drivers-options.js';
import 'typescript-log';
/**
* Interface for a L1 cache driver. Probably a memory driver
*/
interface L1CacheDriver extends CacheDriver<false> {
type: 'l1';
getRemainingTtl(key: string): number | undefined;
}
/**
* Interface for a L2, distributed cache driver.
*/
interface L2CacheDriver extends CacheDriver<true> {
type: 'l2';
}
/**
* Factory result for a cache driver
*/
interface CreateDriverResult<T extends L1CacheDriver | L2CacheDriver> {
options: Record<string, any>;
factory: (config: any) => T;
}
/**
* Contract for a bus driver factory
*/
interface CreateBusDriverResult {
options: BusOptions;
factory: (config: any) => BusDriver;
}
/**
* Cache serializer contract
*/
interface CacheSerializer {
serialize: (value: any) => string;
deserialize: (value: any) => any;
}
/**
* Stack of cache drivers
*/
interface CacheStackDrivers {
l1Driver?: L1CacheDriver;
l2Driver?: L2CacheDriver;
busDriver?: BusDriver;
busOptions?: BusOptions;
}
/**
* A Mastercache Plugin
*/
interface MasterCachePlugin {
register(mastercache: MasterCache<any>): void;
}
/**
* Dialect available for the SQL driver
*/
type DialectName = 'pg' | 'mysql2' | 'better-sqlite3' | 'sqlite3';
declare class MasterStore {
#private;
constructor(baseOptions?: RawCommonOptions & {
prefix?: string;
});
/**
* Add a L1 layer to your store. This is usually a memory driver
* for fast access purposes.
*/
useL1Layer(driver: CreateDriverResult<L1CacheDriver>): this;
/**
* Add a L2 layer to your store. This is usually something
* distributed like Redis, DynamoDB, Sql database, etc.
*/
useL2Layer(driver: CreateDriverResult<L2CacheDriver>): this;
/**
* Add a bus to your store. It will be used to synchronize L1 layers between
* different instances of your application.
*/
useBus(bus: CreateBusDriverResult): this;
get entry(): {
options: RawCommonOptions & {
prefix?: string;
};
l1: CreateDriverResult<L1CacheDriver> | undefined;
l2: CreateDriverResult<L2CacheDriver> | undefined;
bus: CreateBusDriverResult | undefined;
};
}
/**
* Create a new store
*/
declare function masterstore(options?: RawCommonOptions & {
prefix?: string;
}): MasterStore;
declare class MasterCache<KnownCaches extends Record<string, MasterStore>> implements CacheProvider {
#private;
constructor(config: RawMasterCacheOptions & {
default: keyof KnownCaches;
stores: KnownCaches;
plugins?: MasterCachePlugin[];
});
get defaultStoreName(): string;
/**
* Use a registered cache driver
*/
use<CacheName extends keyof KnownCaches>(cache?: CacheName): CacheProvider;
/**
* Subscribe to a given cache event
*/
on<Event extends keyof CacheEvents>(event: Event, callback: (arg: CacheEvents[Event]) => void): this;
/**
* Subscribe to a given cache event only once
*/
once<Event extends keyof CacheEvents>(event: Event, callback: (arg: CacheEvents[Event]) => void): this;
/**
* Unsubscribe the callback from the given event
*/
off<Event extends keyof CacheEvents>(event: Event, callback: (arg: CacheEvents[Event]) => void): this;
/**
* Returns a new instance of the driver namespaced
*/
namespace(namespace: string): CacheProvider;
/**
* Get a value from the cache
*/
get<T = any>(options: GetPojoOptions<T>): Promise<T>;
get<T = any>(key: string): Promise<T | null | undefined>;
get<T = any>(key: string, defaultValue: Factory<T>, options?: GetOptions): Promise<T>;
/**
* Put a value in the cache
* Returns true if the value was set, false otherwise
*/
set(keyOrOptions: string | SetPojoOptions, value?: any, options?: SetOptions): Promise<boolean>;
/**
* Put a value in the cache forever
* Returns true if the value was set, false otherwise
*/
setForever(keyOrOptions: string | SetPojoOptions, value?: any, options?: SetOptions): Promise<boolean>;
/**
* Retrieve an item from the cache if it exists, otherwise store the value
* provided by the factory and return it
*/
getOrSet<T>(keyOrOptions: string | GetOrSetPojoOptions<T>, factory?: GetSetFactory<T>, options?: GetOrSetOptions): Promise<T>;
/**
* Retrieve an item from the cache if it exists, otherwise store the value
* provided by the factory forever and return it
*/
getOrSetForever<T>(key: string | GetOrSetForeverPojoOptions<T>, cb?: GetSetFactory<T>, opts?: GetOrSetForeverOptions): Promise<T>;
/**
* Check if a key exists in the cache
*/
has(keyOrOptions: string | HasPojoOptions, options?: HasOptions): Promise<boolean>;
/**
* Check if key is missing in the cache
*/
missing(keyOrOptions: string | HasPojoOptions, options?: HasOptions): Promise<boolean>;
/**
* Get the value of a key and delete it
*
* Returns the value if the key exists, undefined otherwise
*/
pull<T = any>(key: string): Promise<T | null | undefined>;
/**
* Delete a key from the cache
* Returns true if the key was deleted, false otherwise
*/
delete(keyOrOptions: string | DeletePojoOptions, options?: DeleteOptions): Promise<boolean>;
/**
* Delete multiple keys from the cache
*/
deleteMany(keysOrOptions: string[] | DeleteManyPojoOptions, options?: DeleteOptions): Promise<boolean>;
/**
* Remove all items from the cache
*/
clear(options?: ClearOptions): Promise<void>;
/**
* Remove all items from all caches
*/
clearAll(options?: ClearOptions): Promise<void>;
/**
* Closes the connection to the cache
*/
disconnect(): Promise<void>;
/**
* Disconnect all cache connections created by the manager
*/
disconnectAll(): Promise<void>;
}
export { type CreateDriverResult as C, type DialectName as D, type L1CacheDriver as L, MasterCache as M, type CreateBusDriverResult as a, type L2CacheDriver as b, type CacheSerializer as c, type CacheStackDrivers as d, type MasterCachePlugin as e, MasterStore as f, masterstore as m };