Skip to content

Commit 5a693b0

Browse files
committed
Merge remote-tracking branch 'immorez/feat/prompt-lru-cache'
2 parents a9494f0 + 4e7c131 commit 5a693b0

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-18
lines changed

src/prompts/promptCache.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
1+
import createLRUCache from "../utils/lru-cache";
12

23
// Remove all newlines, double spaces, etc
34
function normalizeText(src: string) {
4-
src = src.split('\n').join(' ');
5-
src = src.replace(/\s+/gm, ' ');
6-
return src;
5+
src = src.split("\n").join(" ");
6+
src = src.replace(/\s+/gm, " ");
7+
return src;
78
}
89

9-
function extractPromptCacheKey(args: { prefix: string, suffix: string | null }) {
10-
if (args.suffix) {
11-
return normalizeText(args.prefix + ' ##CURSOR## ' + args.suffix);
12-
} else {
13-
return normalizeText(args.prefix);
14-
}
10+
function extractPromptCacheKey(args: {
11+
prefix: string;
12+
suffix: string | null;
13+
}) {
14+
if (args.suffix) {
15+
return normalizeText(args.prefix + " ##CURSOR## " + args.suffix);
16+
} else {
17+
return normalizeText(args.prefix);
18+
}
1519
}
1620

17-
// TODO: make it LRU
18-
let cache: { [key: string]: string | null } = {};
21+
const promptCache = createLRUCache<string, string | null>({ maxSize: 1000 });
1922

20-
export function getFromPromptCache(args: { prefix: string, suffix: string | null }): string | undefined | null {
21-
const key = extractPromptCacheKey(args);
22-
return cache[key];
23+
export function getFromPromptCache(args: {
24+
prefix: string;
25+
suffix: string | null;
26+
}): string | undefined | null {
27+
const key = extractPromptCacheKey(args);
28+
return promptCache.get(key);
2329
}
2430

25-
export function setPromptToCache(args: { prefix: string, suffix: string | null, value: string | null }) {
26-
const key = extractPromptCacheKey(args);
27-
cache[key] = args.value;
28-
}
31+
export function setPromptToCache(args: {
32+
prefix: string;
33+
suffix: string | null;
34+
value: string | null;
35+
}) {
36+
const key = extractPromptCacheKey(args);
37+
promptCache.set(key, args.value);
38+
}

src/utils/lru-cache.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
interface LRUCacheConfig<K, V> {
2+
maxSize: number;
3+
}
4+
5+
class LRUCache<K, V> {
6+
private cache: Map<K, V>;
7+
private maxSize: number;
8+
9+
constructor(config: LRUCacheConfig<K, V>) {
10+
this.cache = new Map<K, V>();
11+
this.maxSize = config.maxSize;
12+
}
13+
14+
get(key: K): V | undefined {
15+
if (!this.cache.has(key)) {
16+
return undefined;
17+
}
18+
19+
const value = this.cache.get(key)!;
20+
this.cache.delete(key);
21+
this.cache.set(key, value);
22+
return value;
23+
}
24+
25+
set(key: K, value: V): void {
26+
if (this.cache.has(key)) {
27+
this.cache.delete(key);
28+
}
29+
30+
this.cache.set(key, value);
31+
32+
if (this.cache.size > this.maxSize) {
33+
const oldestKey = this.cache.keys().next().value;
34+
35+
if (oldestKey) {
36+
this.cache.delete(oldestKey);
37+
}
38+
}
39+
}
40+
41+
clear(): void {
42+
this.cache.clear();
43+
}
44+
45+
has(key: K): boolean {
46+
return this.cache.has(key);
47+
}
48+
49+
get size(): number {
50+
return this.cache.size;
51+
}
52+
}
53+
54+
function createLRUCache<K, V>(config: LRUCacheConfig<K, V>): LRUCache<K, V> {
55+
return new LRUCache<K, V>(config);
56+
}
57+
58+
export default createLRUCache;

0 commit comments

Comments
 (0)