import type { CacheEntry } from './domain/CacheEntry'; import type { CacheInfo } from './domain/CacheInfo'; import type { MemorizeBatchOptions } from './domain/MemorizeBatchOptions'; import type { MemorizeDeleteEvent } from './domain/MemorizeDeleteEvent'; import type { MemorizeEmptyEvent } from './domain/MemorizeEmptyEvent'; import type { MemorizeEvent } from './domain/MemorizeEvent'; import { MemorizeEventType } from './domain/MemorizeEventType'; import type { MemorizeEvictEvent } from './domain/MemorizeEvictEvent'; import type { MemorizeExpireEvent } from './domain/MemorizeExpireEvent'; import type { MemorizeInspectionOptions, MemorizeInspectionPage } from './domain/MemorizeInspection'; import type { MemorizeSetEvent } from './domain/MemorizeSetEvent'; import type { MemorizeStats } from './domain/MemorizeStats'; import type { MemorizeStoreLike, MemorizeStoreOptions, StoreEntryInput } from './MemorizeStoreLike'; export type { CacheEntry, CacheInfo, MemorizeDeleteEvent, MemorizeEmptyEvent, MemorizeEvent, MemorizeEvictEvent, MemorizeExpireEvent, MemorizeSetEvent, MemorizeStats, }; export { MemorizeEventType }; export declare const DEFAULT_TTL = 60000; export declare function normalizeTtl(ttl?: number | null): { expiresAt: number | null; }; export declare function normalizeBatchSize(options?: MemorizeBatchOptions): number; export declare function normalizeInspectionOptions(options?: MemorizeInspectionOptions): { batchSize: number; limit: number; offset: number; }; export declare function normalizeByteLimit(name: string, value: number | undefined): number | undefined; /** * Low-level in-memory key-value store with optional TTL, LRU eviction, and event emission. * * You do not usually interact with this class directly — use the {@link memorize} factory * instead, which wraps this store in an Express middleware. */ export declare class MemorizeStore implements MemorizeStoreLike { private _store; private _expiryHeap; private _expiryTimer; private _nextExpiryAt; private _nextExpiryKey; private _totalByteSize; private _hits; private _misses; private _listeners; private readonly _maxEntries?; private readonly _maxValueBytes?; private readonly _maxTotalBytes?; private readonly _sizeLimitAction; constructor(maxEntriesOrOptions?: number | MemorizeStoreOptions); /** * Registers an event listener. * * @param event - The event to listen for. * @param handler - Callback invoked with the event payload. * @returns A function that unregisters the listener. * * @example * ```ts * const unsubscribe = store.on(MemorizeEventType.Set, (e) => console.log('cached', e.key)); * unsubscribe(); * ``` */ on(event: MemorizeEventType.Set, handler: (e: MemorizeSetEvent) => void): () => void; on(event: MemorizeEventType.Delete, handler: (e: MemorizeDeleteEvent) => void): () => void; on(event: MemorizeEventType.Expire, handler: (e: MemorizeExpireEvent) => void): () => void; on(event: MemorizeEventType.Empty, handler: (e: MemorizeEmptyEvent) => void): () => void; on(event: MemorizeEventType.Evict, handler: (e: MemorizeEvictEvent) => void): () => void; /** * Unregisters a previously registered event listener. Unknown handlers are ignored. * * @param event - The event the listener was registered for. * @param handler - The exact handler reference passed to {@link on}. */ off(event: MemorizeEventType.Set, handler: (e: MemorizeSetEvent) => void): void; off(event: MemorizeEventType.Delete, handler: (e: MemorizeDeleteEvent) => void): void; off(event: MemorizeEventType.Expire, handler: (e: MemorizeExpireEvent) => void): void; off(event: MemorizeEventType.Empty, handler: (e: MemorizeEmptyEvent) => void): void; off(event: MemorizeEventType.Evict, handler: (e: MemorizeEvictEvent) => void): void; private _off; /** * Stores an entry in the cache. * * If an entry already exists for the given key its TTL timer is reset and the * value is overwritten. If `maxEntries` is configured and the store is full, * the least-recently-used entry is evicted first. Emits a {@link MemorizeEventType.Set} event. * * @param key - The cache key (typically `req.originalUrl`). * @param entry - The response data to store. * @param ttl - Time-to-live in milliseconds. Omit or pass `null` to use the default TTL. * Pass `Infinity` for no expiry. */ set(key: string, entry: StoreEntryInput, ttl?: number | null): void; /** * Returns the formatted {@link CacheInfo} for the given key, or `null` if the * key does not exist or its TTL has elapsed. * * @param key - The cache key to look up. */ get(key: string): CacheInfo | null; /** * Returns all active (non-expired) cache entries as a key→{@link CacheInfo} map. * Expired entries are lazily evicted during this call. */ getAll(): Record; /** * Async variant of {@link getAll} that yields between batches to reduce * event-loop blocking on large stores. * * @param options - Batch options. * @returns All active cache entries keyed by cache key. */ getAllAsync(options?: MemorizeBatchOptions): Promise>; /** * Returns a bounded page of entry metadata without copying cached bodies or * changing LRU order and lookup statistics. Yields between scan batches. */ inspectAsync(options?: MemorizeInspectionOptions): Promise; /** * Removes a single entry from the cache. Emits a {@link MemorizeEventType.Delete} event. * * @param key - The cache key to remove. * @returns `true` if the entry existed and was removed, `false` otherwise. */ delete(key: string): boolean; /** * Removes all cache entries whose keys match the given glob pattern. * Emits a {@link MemorizeEventType.Delete} event for each removed entry. * * Glob rules: * - `*` — matches any character sequence **within** a single path segment (does not cross `/`). * - `**` — matches any character sequence **across** path segments (crosses `/`). * - `?` — matches any single character except `/`. * * @param pattern - Glob pattern to match against cache keys. * @returns The number of entries removed. */ deleteMatching(pattern: string): number; /** * Async variant of {@link deleteMatching} that yields between batches to * reduce event-loop blocking on large stores. * * @param pattern - Glob pattern to match against cache keys. * @param options - Batch options. * @returns The number of entries removed. */ deleteMatchingAsync(pattern: string, options?: MemorizeBatchOptions): Promise; /** * Removes all cache entries carrying at least one of the given tags. * Emits a {@link MemorizeEventType.Delete} event for each removed entry. * * @param tag - A tag or list of tags to match against entry tags. * @returns The number of entries removed. */ deleteByTag(tag: string | string[]): number; /** * Async variant of {@link deleteByTag} that yields between batches to * reduce event-loop blocking on large stores. * * @param tag - A tag or list of tags to match against entry tags. * @param options - Batch options. * @returns The number of entries removed. */ deleteByTagAsync(tag: string | string[], options?: MemorizeBatchOptions): Promise; /** * Removes all entries from the cache. Emits a {@link MemorizeEventType.Delete} event * for each entry. */ clear(): void; /** * Async variant of {@link clear} that yields between batches to reduce * event-loop blocking on large stores. * * @param options - Batch options. * @returns The number of entries removed. */ clearAsync(options?: MemorizeBatchOptions): Promise; private _takeKeys; /** * Returns the number of active cache entries. */ size(): number; /** * Returns the approximate total byte size of all cached bodies. * * The value is an estimate and may not reflect actual memory usage. */ byteSize(): number; /** * Returns aggregate cache statistics. */ getStats(): MemorizeStats; /** * Returns the raw {@link CacheEntry} for the given key without formatting metadata, * or `null` if the entry is missing or expired. Used internally by the middleware * to serve cached responses. Updates LRU order and increments the hit counter. * * @param key - The cache key to look up. * @internal */ getRaw(key: string): CacheEntry | null; private _evictLRU; private _canStoreSize; private _removeStoredEntry; private _evict; private _scheduleAfterRemoval; private _scheduleNextExpiry; private _scheduleExpiryFor; private _scheduleExpiryAt; private _clearExpiryTimer; private _findNextExpiry; private _rebuildExpiryHeap; private _evictExpiredEntries; private _evictExpiredEntry; /** * Releases all resources held by the store: cancels the shared expiry timer * and drops every entry and listener **without emitting events**. Useful in * tests and graceful shutdowns. The store must not be used after disposal. */ dispose(): void; private _emit; private _format; private _formatMetadata; } //# sourceMappingURL=MemorizeStore.d.ts.map