small fix for completion cache (#130)

add-tracing
vodkaslime 2023-05-12 23:10:57 +08:00 committed by GitHub
parent 75c82fa7c6
commit 666d37ff52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 10 deletions

View File

@ -14,25 +14,23 @@ export type CompletionCacheEntry = {
}; };
export class CompletionCache { export class CompletionCache {
public static cacheSize = 10; public static capacity = 10;
private cache = new LinkedList<CompletionCacheEntry>(); private cache = new LinkedList<CompletionCacheEntry>();
constructor() {} constructor() {}
private evict() { private refresh(entry: CompletionCacheEntry) {
while (this.cache.length > CompletionCache.cacheSize) {
this.cache.removeTail();
}
}
private pop(entry: CompletionCacheEntry) {
this.cache.remove(entry); this.cache.remove(entry);
this.cache.prepend(entry); this.cache.prepend(entry);
} }
public add(entry: CompletionCacheEntry) { public add(entry: CompletionCacheEntry) {
this.evict();
this.cache.prepend(entry); this.cache.prepend(entry);
while (this.cache.length > CompletionCache.capacity) {
this.cache.removeTail();
}
} }
public findCompatible(documentId: any, text: string, cursor: number): CompletionResponse | null { public findCompatible(documentId: any, text: string, cursor: number): CompletionResponse | null {
@ -63,7 +61,7 @@ export class CompletionCache {
} }
} }
if (hit) { if (hit) {
this.pop(hit.entry); this.refresh(hit.entry);
return { return {
id: hit.entry.completion.id, id: hit.entry.completion.id,
created: hit.entry.completion.created, created: hit.entry.completion.created,