Skip to content

Commit ba4e5e3

Browse files
committed
feat(cache): only start on init
1 parent b871514 commit ba4e5e3

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

cache/cache.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package cache
22

33
import (
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

1919
func checkExpiredKeys() {
2020
for key, value := range cacheMap {
@@ -25,41 +25,45 @@ func checkExpiredKeys() {
2525
}
2626

2727
func 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

3433
func 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

4942
func 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+
6061
var ticker *time.Ticker
6162

6263
func 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-
}

handler/handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ func HandleRequestMessage(writer *os.File, message []byte) error {
5353
if err != nil {
5454
return err
5555
}
56+
if params.InitializationOptions.Cache == nil || *params.InitializationOptions.Cache != false {
57+
go cache.Init()
58+
}
5659
output := lsp.NewInitializeResponse(request.ID)
5760
writer.Write(lsp.NewResponseMessage(output))
5861
return nil

lsp/initialize.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type ClientInfo struct {
88
type InitializationOptions struct {
99
Provider string `json:"provider"`
1010
Params any `json:"params"`
11+
Cache *bool `json:"cache,omitempty"`
1112
}
1213

1314
type InitializeParams struct {

main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"flag"
66
"fmt"
7-
"llm-language-server/cache"
87
"llm-language-server/handler"
98
"llm-language-server/jsonrpc"
109
"llm-language-server/lsp"
@@ -28,8 +27,6 @@ func main() {
2827
writer := os.Stdout
2928
writer.Write(lsp.NewNotificationMessage(lsp.NewLogMesssage(fmt.Sprintf("Successfully started! (debug: %t)", *debug), lsp.Info)))
3029

31-
go cache.Init()
32-
3330
for scanner.Scan() {
3431
bytes := scanner.Bytes()
3532
message, err := jsonrpc.DecodeMessage(bytes)

0 commit comments

Comments
 (0)