| 62 | } |
| 63 | |
| 64 | class MemoryCache implements PromptCache { |
| 65 | private store = new Map<string, CacheEntry>(); |
| 66 | |
| 67 | async get(key: string): Promise<CacheEntry | null> { |
| 68 | const e = this.store.get(key); |
| 69 | if (!e) return null; |
| 70 | if (e.expires_at < Date.now()) { |
| 71 | this.store.delete(key); |
| 72 | return null; |
| 73 | } |
| 74 | return e; |
| 75 | } |
| 76 | |
| 77 | async put(key: string, _prompt: string, _model: string, value: unknown, ttl_ms: number, usage: { prompt_tokens?: number; completion_tokens?: number; cost_usd?: number }): Promise<void> { |
| 78 | const now = Date.now(); |
| 79 | this.store.set(key, { |
| 80 | value, |
| 81 | prompt_tokens: usage.prompt_tokens ?? null, |
| 82 | completion_tokens: usage.completion_tokens ?? null, |
| 83 | cost_usd: usage.cost_usd ?? null, |
| 84 | cached_at: now, |
| 85 | expires_at: now + ttl_ms, |
| 86 | }); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | class PgCache implements PromptCache { |
| 91 | async get(key: string): Promise<CacheEntry | null> { |
nothing calls this directly
no outgoing calls
no test coverage detected