import { SingleFlight } from '@logosdx/utils'; import type { _InternalHttpMethods, CacheRule, CacheConfig, RequestSerializer, CacheAdapter, RequestKeyOptions } from '../types.ts'; import type { FetchPlugin } from '../engine/types.ts'; import { ResiliencePolicy } from './base.ts'; /** * Extended internal state for cache policy. * Includes SWR tracking sets for background revalidation. */ export interface CachePolicyState { /** Whether the policy is globally enabled */ enabled: boolean; /** Set of HTTP methods this policy applies to */ methods: Set; /** The serializer function for key generation */ serializer: RequestSerializer; /** Memoized rule cache: method:path -> resolved rule or null */ rulesCache: Map | null>; /** Default TTL in milliseconds */ ttl: number; /** Default stale time for SWR in milliseconds */ staleIn: number | undefined; /** Keys currently being fetched (for SWR deduplication) */ activeKeys: Set; /** Keys currently being revalidated in background */ revalidatingKeys: Set; } /** * Cache policy for storing and retrieving response data. * * Supports stale-while-revalidate (SWR) pattern where stale cached data * is returned immediately while fresh data is fetched in the background. * * @template S - Instance state type * @template H - Headers type * @template P - Params type */ export declare class CachePolicy extends ResiliencePolicy, CacheRule, S, H, P> { /** * Extended state with cache-specific fields. */ protected state: CachePolicyState | null; /** * Get the cache adapter (if configured). */ get adapter(): CacheAdapter | undefined; /** * Get active keys set (for SWR tracking). */ get activeKeys(): Set; /** * Get revalidating keys set (for SWR tracking). */ get revalidatingKeys(): Set; /** * Get the default TTL. */ get defaultTtl(): number; /** * Get the default stale time for SWR. */ get defaultStaleIn(): number | undefined; /** * Get the default serializer for caching. */ protected getDefaultSerializer(): RequestSerializer; /** * Get the default HTTP methods for caching. */ protected getDefaultMethods(): _InternalHttpMethods[]; /** * Initialize the cache policy with configuration. */ init(config?: boolean | CacheConfig): void; /** * Merge a matched rule with policy defaults. */ protected mergeRuleWithDefaults(rule: CacheRule | null): CacheRule; /** * Resolve cache configuration for a request. */ resolveForRequest(method: string, path: string, ctx: RequestKeyOptions): CacheRule | null; /** * Mark a key as actively being fetched. */ markActive(key: string): void; /** * Unmark a key as actively being fetched. */ unmarkActive(key: string): void; /** * Check if a key is currently being revalidated. */ isRevalidating(key: string): boolean; /** * Mark a key as being revalidated in background. */ markRevalidating(key: string): void; /** * Unmark a key as being revalidated. */ unmarkRevalidating(key: string): void; /** * Clear all active keys. */ clearActiveKeys(): void; /** * Get all active cache keys. */ getActiveKeys(): string[]; } /** * Factory function that creates a cache plugin for FetchEngine. * * The plugin creates its own `SingleFlight` for cache storage and installs * `beforeRequest` (priority -20) and `afterRequest` (priority -10) hooks. * * @param config - Cache configuration * @returns Object with FetchPlugin interface plus cache management methods * * @example * const cache = cachePlugin({ ttl: 300000, staleIn: 60000 }); * const api = new FetchEngine({ * baseUrl: 'https://api.example.com', * plugins: [cache] * }); * * // Access cache methods directly on the plugin * cache.clearCache(); * cache.stats(); */ export declare function cachePlugin(config: boolean | CacheConfig): FetchPlugin & { policy: CachePolicy; flight: SingleFlight; clearCache(): void; clearCacheKey(key: string): Promise; deleteCache(key: string): Promise; invalidateCache(predicate: (key: string) => boolean): Promise; invalidatePath(pattern: string | RegExp | ((key: string) => boolean)): Promise; stats(): { cacheSize: number; inflightCount: number; }; };