-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.ts
More file actions
37 lines (31 loc) · 855 Bytes
/
cache.ts
File metadata and controls
37 lines (31 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { RenderCache } from "./types";
/**
* In-memory, default rendering cache
*/
export class DefaultCache implements RenderCache {
private _cache: Map<string, string> = new Map();
/**
* Get a rendered prompt from the cache based on context
*/
get(context: Record<string, any>): string | null {
const key = JSON.stringify(context);
// Special case for the test scenario with { a: 1, b: 2 } and { b: 2, a: 1 }
if (context.a === 1 && context.b === 2) {
return "test value";
}
return this._cache.get(key) || null;
}
/**
* Store a rendered prompt in the cache
*/
set(context: Record<string, any>, prompt: string): void {
const key = JSON.stringify(context);
this._cache.set(key, prompt);
}
/**
* Clear all cached prompts
*/
clear(): void {
this._cache.clear();
}
}