UNPKG

hacker-news-reel

Version:

A lightweight, typed client for the Hacker News API with validation using Zod

66 lines (65 loc) 1.88 kB
/** * Configuration options for the cache */ interface CacheOptions { /** Maximum age in ms for fresh data */ maxAge: number; /** Maximum age in ms for stale data */ staleWhileRevalidate: number; /** Maximum number of entries before LRU eviction */ maxEntries?: number; } /** * In-memory cache with stale-while-revalidate semantics. * - Returns fresh data if within maxAge * - Returns stale data if within staleWhileRevalidate while refreshing in the background * - Fetches new data if beyond staleWhileRevalidate */ export declare class Cache<T> { private cache; private options; private fetchPromises; constructor(options?: Partial<CacheOptions>); /** * Evict the least recently used entries if we exceed the maximum size */ private evictLRUIfNeeded; /** * Get an item from the cache or fetch it * @param key Cache key * @param fetchFn Function to fetch the data if not in cache * @returns The cached or newly fetched data */ get(key: string, fetchFn: () => Promise<T>): Promise<T>; /** * Force a refresh of the cached item * @param key Cache key * @param fetchFn Function to fetch the data * @returns The newly fetched data */ refresh(key: string, fetchFn: () => Promise<T>): Promise<T>; /** * Clear a specific item from the cache * @param key Cache key to clear */ invalidate(key: string): void; /** * Clear the entire cache */ clear(): void; /** * Check if an item exists in the cache * @param key Cache key * @returns True if the item exists in the cache */ has(key: string): boolean; /** * Refresh data in the background without blocking */ private refreshInBackground; /** * Fetch and cache data */ private fetchAndCache; } export {};