UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

162 lines (161 loc) • 4.19 kB
/** * Cache Middleware * Provides response caching for server adapters */ import type { CacheEntry, MiddlewareDefinition, ServerCacheConfig } from "../../types/index.js"; import type { CacheStore } from "../../types/index.js"; /** * Cache entry */ /** * Cache store interface */ /** * In-memory LRU cache store */ export declare class InMemoryCacheStore implements CacheStore { private cache; private accessOrder; private maxSize; constructor(maxSize?: number); get(key: string): Promise<CacheEntry | undefined>; set(key: string, entry: CacheEntry): Promise<void>; delete(key: string): Promise<void>; clear(): Promise<void>; private updateAccessOrder; } /** * Create cache middleware * * Response headers set by this middleware: * - `X-Cache`: "HIT" if served from cache, "MISS" if freshly generated * - `X-Cache-Age`: Seconds since the response was cached (only on HIT) * - `Cache-Control`: Caching directive with max-age (only on MISS) * * @example * ```typescript * const cacheMiddleware = createCacheMiddleware({ * ttlMs: 60 * 1000, // 1 minute * methods: ["GET"], * excludePaths: ["/api/health"], * }); * * server.registerMiddleware(cacheMiddleware); * ``` */ export declare function createCacheMiddleware(config: ServerCacheConfig): MiddlewareDefinition; /** * Create a cache invalidation helper */ export declare function createCacheInvalidator(store: CacheStore): { invalidate: (pattern: string) => Promise<void>; clear: () => Promise<void>; }; /** * Generic LRU (Least Recently Used) Cache * * Provides a simple in-memory cache with LRU eviction policy. * * @example * ```typescript * const cache = new LRUCache<string, number>(100); * * cache.set("key1", 42); * cache.get("key1"); // => 42 * cache.has("key1"); // => true * cache.delete("key1"); * ``` */ export declare class LRUCache<K, V> { private cache; private accessOrder; private maxSize; constructor(maxSize?: number); /** * Get a value from the cache */ get(key: K): V | undefined; /** * Set a value in the cache */ set(key: K, value: V): void; /** * Check if a key exists in the cache */ has(key: K): boolean; /** * Iterate over all keys currently in the cache */ keys(): IterableIterator<K>; /** * Delete a key from the cache */ delete(key: K): boolean; /** * Clear the cache */ clear(): void; /** * Get the current size of the cache */ get size(): number; private updateAccessOrder; } /** * Synchronous response cache store with TTL support * * Designed for caching HTTP responses with automatic expiration. * * @example * ```typescript * const store = new ResponseCacheStore(100, 60000); // 100 entries, 60s TTL * * store.set("GET:/api/users", { status: 200, data: [...] }); * const cached = store.get("GET:/api/users"); * * // Invalidate specific key * store.invalidate("GET:/api/users"); * * // Invalidate by pattern (e.g., all user endpoints) * store.invalidateByPattern("/api/users"); * ``` */ export declare class ResponseCacheStore<T = unknown> { private cache; private ttlMs; constructor(maxSize?: number, ttlMs?: number); /** * Get a value from the cache */ get(key: string): T | undefined; /** * Set a value in the cache */ set(key: string, value: T, ttlMs?: number): void; /** * Check if a key exists and is not expired */ has(key: string): boolean; /** * Invalidate (delete) a specific key */ invalidate(key: string): boolean; /** * Invalidate all keys matching a pattern (substring match or regex) * @param pattern - String to match or RegExp */ invalidateByPattern(pattern: string | RegExp): number; /** * Invalidate all keys matching a pattern (substring match or regex) * @param pattern - String to match or RegExp */ invalidatePattern(pattern: string | RegExp): number; /** * Clear the entire cache */ clear(): void; /** * Get the current size of the cache */ get size(): number; }