small fix for completion cache (#130)
parent
75c82fa7c6
commit
666d37ff52
|
|
@ -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,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue