/** * Unified Cache Utility * * Simple cache-aside pattern for all caching needs. * Supports Cloudflare KV (primary) with graceful fallback to no-op. * * Usage: * const value = await cached('my-key', 300, () => fetchExpensiveData(), kv) * * Or with the class for more control: * const cache = new Cache(kv) * const value = await cache.get('key') ?? await cache.set('key', data, 300) * * NOTE: Upstash Redis has been removed. Use Cloudflare KV instead. */ /** * KV-compatible interface (works with Cloudflare KV) */ export interface KVStore { get(key: string, options?: { type?: "json" | "text"; }): Promise; put(key: string, value: string, options?: { expirationTtl?: number; }): Promise; delete(key: string): Promise; list?(options?: { prefix?: string; }): Promise<{ keys: Array<{ name: string; }>; }>; } /** * Common TTL values (in seconds) */ export declare const TTL: { /** 1 minute */ readonly SHORT: 60; /** 5 minutes */ readonly MEDIUM: 300; /** 15 minutes */ readonly DEFAULT: 900; /** 1 hour */ readonly LONG: 3600; /** 24 hours */ readonly DAY: 86400; /** 7 days */ readonly WEEK: 604800; /** 30 days */ readonly MONTH: 2592000; }; /** * Cache-aside helper function * * Tries to get value from cache, if miss, computes and caches the result. * Gracefully falls back to compute on any cache failure. * * @param key - Cache key (will be used as-is, add your own prefix) * @param ttlSeconds - Time to live in seconds * @param compute - Function to compute the value on cache miss * @param kv - Cloudflare KV namespace * * @example * const user = await cached( * `user:${userId}`, * TTL.MEDIUM, * () => db.query.users.findFirst({ where: eq(users.id, userId) }), * env.CACHE * ) */ export declare function cached(key: string, ttlSeconds: number, compute: () => Promise, kv?: KVStore | null): Promise; /** * Cache class for more control over caching operations */ export declare class Cache { private kv; constructor(kv: KVStore | null); /** * Get a value from cache */ get(key: string): Promise; /** * Set a value in cache */ set(key: string, value: T, ttlSeconds: number): Promise; /** * Delete a value from cache */ del(key: string): Promise; /** * Delete multiple keys by pattern * Note: KV doesn't support pattern deletion natively, so we list and delete */ delPattern(pattern: string): Promise; /** * Cache-aside helper */ cached(key: string, ttlSeconds: number, compute: () => Promise): Promise; } /** * Create a Cache instance from KV namespace */ export declare function createCache(kv?: KVStore | null): Cache; /** * Create a Cache instance from Cloudflare Workers env */ export declare function createCacheFromEnv(env: { CACHE?: KVStore; }): Cache; /** * @deprecated Use KV directly instead */ export declare function getRedis(): null; /** * @deprecated Use KV directly instead */ export declare function getRedisFromEnv(_env: { UPSTASH_REDIS_REST_URL?: string; UPSTASH_REDIS_REST_TOKEN?: string; }): null; //# sourceMappingURL=cache.d.ts.map