@@ -2,9 +2,7 @@ package cache
22
33import (
44 "crypto/sha256"
5- "fmt"
65 "llm-language-server/lsp"
7- "strings"
86 "time"
97)
108
@@ -13,8 +11,10 @@ type CacheValue struct {
1311 Ex time.Time
1412}
1513
16- var cacheMap = make (map [string ]CacheValue )
17- var EX = 60 * time .Second
14+ var cacheMap map [string ]CacheValue
15+ var started = false
16+
17+ var ex = 60 * time .Second
1818
1919func checkExpiredKeys () {
2020 for key , value := range cacheMap {
@@ -25,41 +25,45 @@ func checkExpiredKeys() {
2525}
2626
2727func getKey (prompt string , suffix string ) string {
28- cacheKey := fmt .Sprintf ("%s<FIM_MIDDLE>%s" , prompt , suffix )
2928 cacheKeyHash := sha256 .New ()
30- cacheKeyHash .Write ([]byte (cacheKey ))
29+ cacheKeyHash .Write ([]byte (prompt + "_" + suffix ))
3130 return string (cacheKeyHash .Sum (nil ))
3231}
3332
3433func Set (prompt string , suffix string , value []lsp.CompletionItem ) {
35- key := getKey (prompt , suffix )
36- cacheMap [key ] = CacheValue {Data : value , Ex : time .Now ().Add (EX )}
37- for _ , item := range value {
38- chars := strings .Split (item .InsertText , "" )
39- pendingLine := ""
40- for _ , char := range chars {
41- pendingLine += char
42- item .InsertText = item .InsertText [1 :]
43- pendingKey := getKey (prompt + pendingLine , suffix )
44- cacheMap [pendingKey ] = CacheValue {Data : []lsp.CompletionItem {item }, Ex : time .Now ().Add (EX )}
45- }
34+ if ! started {
35+ return
4636 }
37+
38+ key := getKey (prompt , suffix )
39+ cacheMap [key ] = CacheValue {Data : value , Ex : time .Now ().Add (ex )}
4740}
4841
4942func Get (prompt string , suffix string ) ([]lsp.CompletionItem , bool ) {
43+ if ! started {
44+ return nil , false
45+ }
46+
5047 key := getKey (prompt , suffix )
5148 value , exists := cacheMap [key ]
5249
5350 if exists {
54- return value .Data , exists
51+ return value .Data , true
5552 } else {
56- return nil , exists
53+ return nil , false
5754 }
5855}
5956
57+ func Reset () {
58+ cacheMap = make (map [string ]CacheValue )
59+ }
60+
6061var ticker * time.Ticker
6162
6263func Init () {
64+ Reset ()
65+ started = true
66+
6367 ticker = time .NewTicker (time .Second )
6468 defer ticker .Stop ()
6569
@@ -76,7 +80,3 @@ func Shutdown() {
7680 ticker .Stop ()
7781 }
7882}
79-
80- func Reset () {
81- cacheMap = make (map [string ]CacheValue )
82- }
0 commit comments