Skip to content

Commit a5180a7

Browse files
committed
fix(cache): use list and check partial prompt/suffix
1 parent 7589200 commit a5180a7

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

cache/cache.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
package cache
22

33
import (
4+
"container/list"
45
"llm-language-server/lsp"
6+
"strings"
57
"time"
68
)
79

8-
type CacheValue struct {
9-
Data []lsp.CompletionItem
10-
Ex time.Time
10+
type Cache struct {
11+
Prompt string
12+
Suffix string
13+
Data []lsp.CompletionItem
14+
Ex time.Time
1115
}
1216

13-
var cacheMap map[string]CacheValue
17+
var cache *list.List
1418
var started = false
1519

16-
var ex = 60 * time.Second
20+
var ex = 600 * time.Second
1721

1822
func checkExpiredKeys() {
19-
for key, value := range cacheMap {
23+
for e := cache.Front(); e != nil; e = e.Next() {
24+
value := e.Value.(Cache)
2025
if time.Now().After(value.Ex) {
21-
delete(cacheMap, key)
26+
cache.Remove(e)
2227
}
2328
}
2429
}
@@ -28,25 +33,36 @@ func Set(prompt string, suffix string, value []lsp.CompletionItem) {
2833
return
2934
}
3035

31-
cacheMap[prompt+suffix] = CacheValue{Data: value, Ex: time.Now().Add(ex)}
36+
cache.PushBack(Cache{Data: value, Prompt: prompt, Suffix: suffix, Ex: time.Now().Add(ex)})
3237
}
3338

3439
func Get(prompt string, suffix string) ([]lsp.CompletionItem, bool) {
3540
if !started {
3641
return nil, false
3742
}
3843

39-
value, exists := cacheMap[prompt+suffix]
40-
41-
if exists {
42-
return value.Data, true
43-
} else {
44-
return nil, false
44+
for e := cache.Front(); e != nil; {
45+
next := e.Next()
46+
value := e.Value.(Cache)
47+
for _, item := range value.Data {
48+
if strings.HasPrefix(value.Prompt+item.InsertText, prompt) && strings.HasSuffix(suffix, value.Suffix) {
49+
clonedData := make([]lsp.CompletionItem, len(value.Data))
50+
for i, item := range value.Data {
51+
clonedItem := item
52+
clonedItem.InsertText = item.InsertText[len(prompt)-len(value.Prompt):]
53+
clonedData[i] = clonedItem
54+
}
55+
return clonedData, true
56+
}
57+
}
58+
e = next
4559
}
60+
61+
return []lsp.CompletionItem{}, false
4662
}
4763

4864
func Reset() {
49-
cacheMap = make(map[string]CacheValue)
65+
cache = list.New()
5066
}
5167

5268
var ticker *time.Ticker

0 commit comments

Comments
 (0)