diff --git a/src/prompts/promptCache.ts b/src/prompts/promptCache.ts index d4b4d73..e278fad 100644 --- a/src/prompts/promptCache.ts +++ b/src/prompts/promptCache.ts @@ -1,28 +1,38 @@ +import createLRUCache from "../utils/lru-cache"; // Remove all newlines, double spaces, etc function normalizeText(src: string) { - src = src.split('\n').join(' '); - src = src.replace(/\s+/gm, ' '); - return src; + src = src.split("\n").join(" "); + src = src.replace(/\s+/gm, " "); + return src; } -function extractPromptCacheKey(args: { prefix: string, suffix: string | null }) { - if (args.suffix) { - return normalizeText(args.prefix + ' ##CURSOR## ' + args.suffix); - } else { - return normalizeText(args.prefix); - } +function extractPromptCacheKey(args: { + prefix: string; + suffix: string | null; +}) { + if (args.suffix) { + return normalizeText(args.prefix + " ##CURSOR## " + args.suffix); + } else { + return normalizeText(args.prefix); + } } -// TODO: make it LRU -let cache: { [key: string]: string | null } = {}; +const promptCache = createLRUCache({ maxSize: 1000 }); -export function getFromPromptCache(args: { prefix: string, suffix: string | null }): string | undefined | null { - const key = extractPromptCacheKey(args); - return cache[key]; +export function getFromPromptCache(args: { + prefix: string; + suffix: string | null; +}): string | undefined | null { + const key = extractPromptCacheKey(args); + return promptCache.get(key); } -export function setPromptToCache(args: { prefix: string, suffix: string | null, value: string | null }) { - const key = extractPromptCacheKey(args); - cache[key] = args.value; -} \ No newline at end of file +export function setPromptToCache(args: { + prefix: string; + suffix: string | null; + value: string | null; +}) { + const key = extractPromptCacheKey(args); + promptCache.set(key, args.value); +} diff --git a/src/utils/lru-cache.ts b/src/utils/lru-cache.ts new file mode 100644 index 0000000..abb8a7f --- /dev/null +++ b/src/utils/lru-cache.ts @@ -0,0 +1,58 @@ +interface LRUCacheConfig { + maxSize: number; +} + +class LRUCache { + private cache: Map; + private maxSize: number; + + constructor(config: LRUCacheConfig) { + this.cache = new Map(); + this.maxSize = config.maxSize; + } + + get(key: K): V | undefined { + if (!this.cache.has(key)) { + return undefined; + } + + const value = this.cache.get(key)!; + this.cache.delete(key); + this.cache.set(key, value); + return value; + } + + set(key: K, value: V): void { + if (this.cache.has(key)) { + this.cache.delete(key); + } + + this.cache.set(key, value); + + if (this.cache.size > this.maxSize) { + const oldestKey = this.cache.keys().next().value; + + if (oldestKey) { + this.cache.delete(oldestKey); + } + } + } + + clear(): void { + this.cache.clear(); + } + + has(key: K): boolean { + return this.cache.has(key); + } + + get size(): number { + return this.cache.size; + } +} + +function createLRUCache(config: LRUCacheConfig): LRUCache { + return new LRUCache(config); +} + +export default createLRUCache;