UNPKG

mastercache

Version:

Multi-tier cache module for Node.js. Redis, Upstash, CloudfareKV, File, in-memory and others drivers

124 lines (115 loc) 3.31 kB
import { CacheBusMessage, CacheBusMessageType } from './src/types/bus.cjs'; /** * Event emitted when a cache entry is hit */ declare class CacheHit implements CacheEvent { readonly key: string; readonly value: any; readonly store: string; readonly graced: boolean; name: "cache:hit"; constructor(key: string, value: any, store: string, graced?: boolean); toJSON(): { key: string; value: any; store: string; graced: boolean; }; } /** * Event emitted when a cache entry is missed */ declare class CacheMiss implements CacheEvent { readonly key: string; readonly store: string; name: "cache:miss"; constructor(key: string, store: string); toJSON(): { key: string; store: string; }; } /** * Event emitted when a cache entry is written * using `set`,`getOrSet` */ declare class CacheWritten implements CacheEvent { readonly key: string; readonly value: any; readonly store: string; name: "cache:written"; constructor(key: string, value: any, store: string); toJSON(): { key: string; store: string; value: any; }; } /** * Event emitted when a cache store is cleared * using `.clear()` */ declare class CacheCleared implements CacheEvent { readonly store: string; name: "cache:cleared"; constructor(store: string); toJSON(): { store: string; }; } /** * Event emitted when a cache entry is deleted * using `.delete()` or `.deleteMany()` */ declare class CacheDeleted implements CacheEvent { readonly key: string; readonly store: string; name: "cache:deleted"; constructor(key: string, store: string); toJSON(): { key: string; store: string; }; } /** * Event when the bus publishes a message */ declare class BusMessagePublished implements CacheEvent { readonly message: CacheBusMessage; name: "bus:message:published"; constructor(message: CacheBusMessage); toJSON(): { keys: string[]; type: CacheBusMessageType; }; } /** * Shape of the emitter accepted by MasterCache * Should be compatible with node's EventEmitter and Emittery */ interface Emitter { on: (event: string, callback: (...values: any[]) => void) => void; once: (event: string, callback: (...values: any[]) => void) => void; off: (event: string, callback: (...values: any[]) => void) => void; emit: (event: string, ...values: any[]) => void; } /** * Name/payload of the events emitted by the cache emitter */ type CacheEvents = { 'cache:cleared': ReturnType<CacheCleared['toJSON']>; 'cache:deleted': ReturnType<CacheDeleted['toJSON']>; 'cache:hit': ReturnType<CacheHit['toJSON']>; 'cache:miss': ReturnType<CacheMiss['toJSON']>; 'cache:written': ReturnType<CacheWritten['toJSON']>; 'bus:message:published': ReturnType<BusMessagePublished['toJSON']>; 'bus:message:received': ReturnType<BusMessagePublished['toJSON']>; }; /** * A cache event */ interface CacheEvent { name: keyof CacheEvents; toJSON: () => Record<string, any>; } export { BusMessagePublished as B, CacheHit as C, type Emitter as E, CacheMiss as a, CacheCleared as b, CacheDeleted as c, CacheWritten as d, type CacheEvent as e, type CacheEvents as f };