UNPKG

cf-workers-query

Version:

Automatically cache and revalidate data in Cloudflare Workers. Using the Cache API and Execution Context

76 lines (72 loc) 2.27 kB
import { Q as QueryKey } from './create-query-BaMMwngc.js'; export { C as CacheApiAdaptor, c as createQuery } from './create-query-BaMMwngc.js'; /** * Global deduplication manager for query execution * * Uses CacheApiAdaptor for distributed deduplication across worker requests/instances. * * Benefits: * - Prevents redundant queries across multiple worker instances via distributed cache * - Short-lived cache entries (5s) minimize storage while providing effective deduplication * - Uses CacheApiAdaptor for consistent cache management across the library * - No global state or timers - works within Cloudflare Workers constraints */ declare class DedupeManager { private readonly CACHE_LOCK_TTL; private lockCache; private resultCache; constructor(); /** * Deduplicate async function execution * * If the same key is requested by multiple workers/requests concurrently: * 1. First request acquires lock and executes the function * 2. Subsequent requests wait for the result from cache * * @param key - Unique identifier for the operation (QueryKey or string) * @param fn - Async function to deduplicate * @returns Result of the function execution */ dedupe<T>(key: QueryKey | string, fn: () => Promise<T>): Promise<T>; /** * Try to acquire a distributed lock via CacheApiAdaptor */ private tryAcquireLock; /** * Release the distributed lock */ private releaseLock; /** * Execute function with lock held */ private executeWithLock; /** * Wait for another worker/request to complete the operation */ private waitForResult; /** * Store result in cache for other workers */ private cacheResult; /** * Get cached result from another worker */ private getCachedResult; /** * Check if a result represents an error */ private isErrorResult; /** * Build lock key for cache */ private buildLockKey; /** * Build result key for cache */ private buildResultKey; } declare const invalidateQuery: ({ queryKey, cacheName, }: { queryKey: QueryKey; cacheName?: string; }) => Promise<void>; export { DedupeManager, invalidateQuery };